<?php
$cookie_file = tempnam('./temp','cookie'); //创建cookie文件保存的位置
/**
* [curl description]
* @param [type] $url [采集地址]
* @param [type] $method [方法]
* @param array $data [数组默认为空]
* @param boolean $setcookie [是否用cookie]
* @param boolean $cookie_file [cookie路径]
* @return [type] [description]
*/
function curl($url,$method,$data=array(),$setcookie=false,$cookie_file=false){
$ch = curl_init();//1.初始化
curl_setopt($ch, CURLOPT_URL, $url); //2.请求地址
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $method);//3.请求方式
//4.参数如下禁止服务器端的验证
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
//伪装请求来源,绕过防盗
//curl_setopt($ch,CURLOPT_REFERER,"http://wthrcdn.etouch.cn/");
//配置curl解压缩方式(默认的压缩方式)
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Accept-Encoding:gzip'));
curl_setopt($ch, CURLOPT_ENCODING, "gzip");
curl_setopt($ch, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']);
//指明以哪种方式进行访问,利用$_SERVER['HTTP_USER_AGENT'],可以获取
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_AUTOREFERER, 1);
if($method=="POST"){//5.post方式的时候添加数据
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
}
if($setcookie==true){
//如果设置要请求的cookie,那么把cookie值保存在指定的文件中
curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie_file);
}else{
//就从文件中读取cookie的信息
curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie_file);
}
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$tmpInfo = curl_exec($ch);//获取html内容
if (curl_errno($ch)) {
return curl_error($ch);
}
curl_close($ch);
return $tmpInfo;
}