curl常用的几个例子

1、抓取无访问控制文件

 <?php
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "http://localhost/mytest/phpinfo.php");
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); //如果把这行注释掉的话,就会直接输出
$result=curl_exec($ch);
curl_close($ch);
?>

2、使用代理进行抓取

<?php
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "http://blog.snsgou.com");
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
&nbsp;curl_setopt($ch, CURLOPT_HTTPPROXYTUNNEL, TRUE);
&nbsp;curl_setopt($ch, CURLOPT_PROXY, 125.21.23.6:8080);
&nbsp;//url_setopt($ch, CURLOPT_PROXYUSERPWD, 'user:password');如果要密码的话,加上这个
$result=curl_exec($ch);
curl_close($ch);
?>

3、post数据后,抓取数据

<?php
$ch = curl_init();
$data = array('name' => 'test', 'sex' => 1);//array(1)
curl_setopt($ch, CURLOPT_URL, 'http://localhost/mytest/curl/userinfo.php');
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_exec($ch);
?>

4、抓取一些有页面访问控制的页面

<?php
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "http://club-china");
curl_setopt($ch, CURLOPT_USERPWD, '[username]:[password]');//破解页面访问控制
curl_setopt($ch, CURLOPT_HTTPGET, 1);
curl_setopt($ch, CURLOPT_REFERER, "http://club-china");
curl_setopt($ch, CURLOPT_HEADER, 0);
$result = curl_exec($ch);
curl_close($ch);
?>

5、模拟登录

<?php
function checkLogin($user, $password){
if (empty($user) || empty($password)){
return false;
}
$ch = curl_init();
curl_setopt($ch, CURLOPT_REFERER, "http://mail.sina.com.cn/index.html");
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_USERAGENT, USERAGENT);
curl_setopt($ch, CURLOPT_COOKIEJAR, COOKIEJAR);
curl_setopt($ch, CURLOPT_TIMEOUT, TIMEOUT);
curl_setopt($ch, CURLOPT_URL, "http://mail.sina.com.cn/cgi-bin/login.cgi");
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, "&logintype=uid&u=" . urlencode($user) . "&psw=" . $password);
$contents = curl_exec($ch);
curl_close($ch);
if (!preg_match("/Location: (.*)\\/cgi\\/index\\.php\\?check_time=(.*)\n/", $contents, $matches)){
return false;
}else{
return true;
}
}
define("USERAGENT", $_SERVER['HTTP_USER_AGENT']);
define("COOKIEJAR", tempnam("c:\windwos\temp", "cookie"));
define("TIMEOUT", 500);
echo checkLogin("username", "password");
?>

6、文件上传

<?php
/**
* @param string $target_url 上传目标地址
* @param string $filename 上传文件路径
* @param string $form_name 表单名称
*/
function curlUploadFile($target_url, $filename, $form_name) {
$upload_file = new CURLFile($filename);
$post_data = array(
$form_name => $upload_file
); $ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $target_url);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); $result = curl_exec($ch);
curl_close($ch);
} $target_url = 'http://www.codean.net/notFound/test.php';
$filename = realpath("C:/Users/HelloWorld/Desktop/Images/1.jpg");
$form_name = 'file'; // 接收端使用$_FILES接受
curlUploadFile($target_url, $filename, $form_name);
?>

7、文件流上传

/*
* 第三种写法,使用PHP流发送
* @param string $target_url 上传目标地址
*/
function curlUploadFile($target_url) {
$fh = fopen('php://temp', 'rw+');
$string = 'Hello World';
fwrite($fh, $string);
rewind($fh); $ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $target_url);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 2);
curl_setopt($ch, CURLOPT_PUT, true);
curl_setopt($ch, CURLOPT_INFILE, $fh);
curl_setopt($ch, CURLOPT_INFILESIZE, strlen($string));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); $result = curl_exec($ch);
curl_close($ch);
}
$target_url = 'http://www.codean.net/notFound/test.php';
curlUploadFile($target_url); // 接收端取出流文件并保存
$putdata = fopen('php://input', 'r');
$fp = fopen('test.txt', 'w');
while ($data = fread($putdata, 1024)) {
fwrite($fp, $data);
}
fclose($fp);
fclose($putdata);

PHP中使用CURL(六)的更多相关文章

  1. php中的curl使用入门教程和常见用法实例

    摘要: [目录] php中的curl使用入门教程和常见用法实例 一.curl的优势 二.curl的简单使用步骤 三.错误处理 四.获取curl请求的具体信息 五.使用curl发送post请求 六.文件 ...

  2. PHP中使用CURL请求页面,使用fiddler进行抓包

    在PHP中使用CURL访问页面: <?php $ch = curl_init('http://www.baidu.com'); curl_setopt($ch, CURLOPT_RETURNTR ...

  3. PHP中使用cURL实现Get和Post请求的方法

    1.cURL介绍  cURL 是一个利用URL语法规定来传输文件和数据的工具,支持很多协议,如HTTP.FTP.TELNET等.最爽的是,PHP也支持 cURL 库.本文将介绍 cURL 的一些高级特 ...

  4. PHP中的CURL函数库

    PHP中的CURL函数库(Client URL Library Function) curl_close — 关闭一个curl会话curl_copy_handle — 拷贝一个curl连接资源的所有内 ...

  5. PHP中使用cURL

    1.cURL介绍 cURL 是一个利用URL语法规定来传输文件和数据的工具,支持很多协议,如HTTP.FTP.TELNET等.最爽的是,PHP也支持 cURL 库.本文将介绍 cURL 的一些高级特性 ...

  6. 在PHP中使用CURL,“撩”服务器只需几行——php curl详细解析和常见大坑

    在PHP中使用CURL,"撩"服务器只需几行--php curl详细解析和常见大坑 七夕啦,作为开发,妹子没得撩就"撩"下服务器吧,妹子有得撩的同学那就左拥妹子 ...

  7. PHP中使用CURL之php curl详细解析

    在正式讲怎么用之前啊,先提一句,你得先在你的PHP环境中安装和启用curl模块,具体方式我就不讲了,不同系统不同安装方式,可以google查一下,或者查阅PHP官方的文档,还挺简单的. 1. 拿来先试 ...

  8. 在PHP中使用CURL,“撩”服务器只需几行

    在PHP中使用CURL,“撩”服务器只需几行https://segmentfault.com/a/1190000006220620 七夕啦,作为开发,妹子没得撩就“撩”下服务器吧,妹子有得撩的同学那就 ...

  9. PHP中使用CURL实现GET和POST请求数据

    PHP中使用CURL实现GET和POST请求 一.什么是CURL? cURL 是一个利用URL语法规定来传输文件和数据的工具,支持很多协议,如HTTP.FTP.TELNET等.最爽的是,PHP也支持 ...

  10. Error【0007】:zabbix中因为curl版本过低而无法发送邮件

    1. 错误背景 在centos6.5上,源码部署zabbix最新版本zabbix-3.2.14.部署后之后,在配置邮件发送报警时出错 2. 错误提示 3. 原因分析 从网上检索的结果是说,系统中的cu ...

随机推荐

  1. IMAX公司CEO:进军VR产业,打造VR体验中心

    591ARVR资讯网www.591arvr.com报道近日,<财富>杂志采访了IMAX首席执行官理查德·葛尔方(Richar Gelfond),后者谈了谈虚拟现实和IMAX正在打造的VR影 ...

  2. 又遇Release编译的一坑 -- 应用程序正常初始化(0xc000007b)失败。请单击“确定”,终止应用程序。

    项目中使用了xlslib库,以动态库形式编译,由于它没有生成链接库lib文件,所以官方提供的demo中有createDLL这个小程序用来生成lib文件.然而我又 no zuo no die了一次.   ...

  3. get请求与post请求之间的差异

    GET:常用于向服务器请求查询某些信息 get请求适用于当URL完全指定请求资源. get请求不会对数据库进行任何操作相当于数据库的查询.  当进行字段查询时可将查询字段增加到url的末尾. get请 ...

  4. Spring in Action --- 使用MockMvc时报异常

    今天在学习spring时模仿了书上的代码编写基于mockmvc的测试用例,但是运行时报 Error:(8, 8) java: 无法访问javax.servlet.ServletException   ...

  5. RedHat9.0下载地址

    RedHat下载:http://archive.download.redhat.com/pub/redhat/linux/9/en/iso/i386/

  6. jquery 使用attr() 函数对复选框无效的原因

     复选框是网站开发的时候经常用到的网页标签之一,常见的在页面上对复选框的操作包括取值和修改复选框的状态.在jquery中,常见的操作标签的值得函数为attr,然而在操作复选框的时候,通常采用的却是pr ...

  7. Windows Search Service

    Windows Search Service是一个全方位的托管云服务,可以允许开发者通过.Net SDK或者REST API多种多样的搜索服务. 如果你想开发一个搜索服务,那么你的服务应该包含以下组件 ...

  8. hibernate增删改查

    -----------增加--------- public void insertUsers(String userName,String userPwd) { Users u=new Users() ...

  9. 6. Shell 流程控制

    1. 条件选择流程 1.1 if #!/bin/bash # if 格式 #if condition #then # command1 # command2 # ... # commandN #fi ...

  10. CodeForces 566D 并查集集合合并

    #include <stdio.h> #include <algorithm> #define MAX 100000 #define LL long long #define ...