<?php
$cookie_file = tempnam('./temp','cookie'); //創(chuàng)建cookie文件保存的位置
/**
* [curl description]
* @param [type] $url [采集地址]
* @param [type] $method [方法]
* @param array $data [數(shù)組默認(rèn)為空]
* @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.請(qǐng)求地址
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $method);//3.請(qǐng)求方式
//4.參數(shù)如下禁止服務(wù)器端的驗(yàn)證
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
//偽裝請(qǐng)求來源,繞過防盜
//curl_setopt($ch,CURLOPT_REFERER,"http://wthrcdn.etouch.cn/");
//配置curl解壓縮方式(默認(rèn)的壓縮方式)
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Accept-Encoding:gzip'));
curl_setopt($ch, CURLOPT_ENCODING, "gzip");
curl_setopt($ch, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']);
//指明以哪種方式進(jìn)行訪問,利用$_SERVER['HTTP_USER_AGENT'],可以獲取
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_AUTOREFERER, 1);
if($method=="POST"){//5.post方式的時(shí)候添加數(shù)據(jù)
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
}
if($setcookie==true){
//如果設(shè)置要請(qǐng)求的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內(nèi)容
if (curl_errno($ch)) {
return curl_error($ch);
}
curl_close($ch);
return $tmpInfo;
}
浙公網(wǎng)安備 33010602011771號(hào)