php的CURL简单实例
2015-08-17 14:26
579 查看
php的CURL可以实现模拟登录,以及上传,采集数据等等
采集之前先看页面的编码建议同意转成utf-8,使用函数icon
$return = iconv("gbk","UTF-8",$return);//将gbk转成ut-8
下面两个简单的get和post方法
/*
* curl_get请求
*$url 响应的的连接
*/
function C_get($url)
{
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);//目标连接
curl_setopt($ch, CURLOPT_TIMEOUT, 300);//最长时间秒
curl_setopt($ch, CURLOPT_HEADER, 0);//启用时会将头文件的信息作为数据流输出。
curl_setopt($ch, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4 );//提高curl_exec的执行效率
$cip = '123.125.68.' . mt_rand(0, 254);
$xip = '125.90.88.' . mt_rand(0, 254);
$header = array('CLIENT-IP:'.$cip, 'X-FORWARDED-FOR:'.$xip);
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);//伪造ip
curl_setopt($ch, CURLOPT_REFERER, "http://www.baidu.com"); //构造来路
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);//将curl_exec()获取的信息以文件流的形式返回,而不是直接输出
$return = curl_exec($ch);
curl_close($ch);
return $return;
}
/*
* curl_post请求
* $url 响应的的连接
* $data 提交的数据
*/
function C_post($url,$data)
{
$ch = curl_init();
$cip = '123.125.68.' . mt_rand(0, 254);
$xip = '125.90.88.' . mt_rand(0, 254);
$header = array('CLIENT-IP:'.$cip, 'X-FORWARDED-FOR:'.$xip);
curl_setopt($ch, CURLOPT_URL, $url);//目标连接
curl_setopt($ch, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4 );//提高curl_exec的执行效率
curl_setopt($ch, CURLOPT_TIMEOUT, 300);//最长时间秒
curl_setopt($ch, CURLOPT_HEADER, 0);//启用时会将头文件的信息作为数据流输出。
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);//伪造ip
curl_setopt($ch, CURLOPT_REFERER, "http://www.baidu.com"); //构造来路
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);//将curl_exec()获取的信息以文件流的形式返回,而不是直接输出
curl_setopt($ch,CURLOPT_POST,1);//设置为POST方式
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);//POST数据
$return = curl_exec($ch);
curl_close($ch);
return $return;
}
php的curl+post+json数据
注意:把类型改成json
重定向参数有时候会失效。
采集之前先看页面的编码建议同意转成utf-8,使用函数icon
$return = iconv("gbk","UTF-8",$return);//将gbk转成ut-8
下面两个简单的get和post方法
/*
* curl_get请求
*$url 响应的的连接
*/
function C_get($url)
{
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);//目标连接
curl_setopt($ch, CURLOPT_TIMEOUT, 300);//最长时间秒
curl_setopt($ch, CURLOPT_HEADER, 0);//启用时会将头文件的信息作为数据流输出。
curl_setopt($ch, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4 );//提高curl_exec的执行效率
$cip = '123.125.68.' . mt_rand(0, 254);
$xip = '125.90.88.' . mt_rand(0, 254);
$header = array('CLIENT-IP:'.$cip, 'X-FORWARDED-FOR:'.$xip);
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);//伪造ip
curl_setopt($ch, CURLOPT_REFERER, "http://www.baidu.com"); //构造来路
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);//将curl_exec()获取的信息以文件流的形式返回,而不是直接输出
$return = curl_exec($ch);
curl_close($ch);
return $return;
}
/*
* curl_post请求
* $url 响应的的连接
* $data 提交的数据
*/
function C_post($url,$data)
{
$ch = curl_init();
$cip = '123.125.68.' . mt_rand(0, 254);
$xip = '125.90.88.' . mt_rand(0, 254);
$header = array('CLIENT-IP:'.$cip, 'X-FORWARDED-FOR:'.$xip);
curl_setopt($ch, CURLOPT_URL, $url);//目标连接
curl_setopt($ch, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4 );//提高curl_exec的执行效率
curl_setopt($ch, CURLOPT_TIMEOUT, 300);//最长时间秒
curl_setopt($ch, CURLOPT_HEADER, 0);//启用时会将头文件的信息作为数据流输出。
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);//伪造ip
curl_setopt($ch, CURLOPT_REFERER, "http://www.baidu.com"); //构造来路
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);//将curl_exec()获取的信息以文件流的形式返回,而不是直接输出
curl_setopt($ch,CURLOPT_POST,1);//设置为POST方式
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);//POST数据
$return = curl_exec($ch);
curl_close($ch);
return $return;
}
curl_setopt($ch , CURLOPT_SSL_VERIFYPEER , false);加上400请求错误,不加上httpcode为空(0)
//$header[] = "User-Agent : Mozilla/5.0 (Windows NT 6.3; WOW64; rv:40.0) Gecko/20100101 Firefox/40.0"; //curl_setopt($ch , CURLOPT_HTTPHEADER , $header);//用header报400错误??? 对URL参数做转码处理,以免出错
rawurlencode();
http_build_query($post)//post为数组
php的curl+post+json数据
注意:把类型改成json
$ch = curl_init($url); curl_setopt($ch , CURLOPT_FAILONERROR , true); $header[] = "Referer : https://www.cs.com/login"; $header[] = "User-Agent : Mozilla/5.0 (Windows NT 6.3; WOW64; rv:40.0) Gecko/20100101 Firefox/40.0"; $header[] = "Content-Type: application/json";//重点 $header[] = "Content-Length: " . strlen($pstr); curl_setopt($ch , CURLOPT_HEADER , 0); curl_setopt($ch , CURLOPT_RETURNTRANSFER , 1);//不输出 curl_setopt($ch , CURLOPT_POST , 1); curl_setopt($ch , CURLOPT_FOLLOWLOCATION , 1); //自动跟踪location curl_setopt($ch , CURLOPT_POSTFIELDS , $pstr);//json数据 // //curl_setopt($ch , CURLOPT_HTTPHEADER , $header);//重点:把类型改成json $res = curl_exec($ch); $info = curl_getinfo($ch); //var_dump($info); curl_close($ch);
重定向参数有时候会失效。
curl_setopt($city_ch , CURLOPT_MAXREDIRS , 99); //最大重定向次数(依旧失效)获取重定向连接再次访问,还是重定向。怀疑代码防止curl抓取的问题
curl_setopt($ch , CURLOPT_COOKIEJAR , $this->cookie_file);//把cookie写入文件,只要路径就好,会自动创建文件。
相关文章推荐
- PHP与JSON
- phpexcel中文手册
- php随机数生成函数示例
- 解决PHP执行乱码、每次都要手动更改Firefox Unicode设置问题
- PHP获取表单方法
- php.ini文件中的include_path设置
- thinkphp的select和find的区别
- php读文件——将服务端的文件读出来显示在web页面
- output
- 浅谈PHP中6种GET和POST请求发送方法
- windows 2008 FTP 服务器搭建
- php设计模式之观察者模式
- php sql和sqli的区别
- PHP图片压缩解决方法
- php session 购物车类
- php中switch语句用法详解
- php des和 java des
- php面向对象要点[转]
- php图片水印添加,压缩,剪切的封装类
- nohup php -f xx.php &