学习整理php使用curl函数库获取网页cookie和带cookie去发起请求的方法
获取cookie
function get_cookie($url_,$params_,$referer_){
if($url_==null){echo "get_cookie_url_null";exit;}
if($params_==null){echo "get_params_null";exit;}
if($referer_==null){echo "get_referer-null";exit;}
$this_header = array("content-type: application/x-www-form-urlencoded; charset=UTF-8");
$ch = curl_init($url_);
curl_setopt($ch,CURLOPT_HTTPHEADER,$this_header);
curl_setopt($ch, CURLOPT_HEADER,1);
curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch,CURLOPT_POST,1);
curl_setopt($ch,CURLOPT_POSTFIELDS,$params_);
curl_setopt($ch, CURLOPT_COOKIEJAR, 'cookie.txt');
curl_setopt ($ch, CURLOPT_REFERER,$referer_);
$content=curl_exec($ch);
if(curl_errno($ch)){
echo 'Curl error: '.curl_error($ch);exit();
}
if($content==false){
echo "get_content_null";exit();
}
preg_match('/Set-Cookie:(.*);/iU',$content,$str);
$cookie = $str[1];
curl_close($ch);
return $cookie;
}
post参数带上cookie
function post($url,$post_data,$location = 0,$reffer = null,$origin = null,$host = null){
$post_data = is_array($post_data)?http_build_query($post_data):$post_data;
$header = array(
'Accept:*/*',
'Accept-Charset:text/html,application/xhtml+xml,application/xml;q=0.7,*;q=0.3',
'Accept-Encoding:gzip,deflate,sdch',
'Accept-Language:zh-CN,zh;q=0.8',
'Connection:keep-alive',
'Content-Type:application/x-www-form-urlencoded',
);
if($host){
$header = array_merge_recursive($header,array("Host:".$host));
}
else if($this->option["host"]){
$header = array_merge_recursive($header,array("Host:".$this->option["host"]));
}
if($origin){
$header = array_merge_recursive($header,array("Origin:".$origin));
}
else{
$header = array_merge_recursive($header,array("Origin:".$url));
}
if($reffer){
$header = array_merge_recursive($header,array("Referer:".$reffer));
}
else{
$header = array_merge_recursive($header,array("Referer:".$url));
}
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_HTTPHEADER, $header);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 1);
curl_setopt($curl, CURLOPT_USERAGENT, $this->useragent);
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, $location);
curl_setopt($curl, CURLOPT_AUTOREFERER, 1);
curl_setopt($curl, CURLOPT_POST, 1);
curl_setopt($curl, CURLOPT_ENCODING, "gzip" );
curl_setopt($curl, CURLOPT_POSTFIELDS, $post_data);
curl_setopt($curl, CURLOPT_COOKIEJAR, 'cookie.txt');
curl_setopt($curl, CURLOPT_COOKIEFILE, 'cookie.txt');
curl_setopt($curl, CURLOPT_TIMEOUT, 30);
curl_setopt($curl, CURLOPT_HEADER, 1);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
$tmpInfo = curl_exec($curl);
if (curl_errno($curl)) {
echo 'Curl error: ' . curl_error ( $curl );exit();
}
curl_close($curl);
list($header, $body) = explode("\r\n\r\n", $tmpInfo, 2);
$tmpInfo = $this->auto_charest($tmpInfo);
return array("header"=>$header,"body"=>$body,"content"=>$tmpInfo);
}
get请求带上cookie
function get($url,$location = 1,$origin = null,$reffer = null,$host = null){
if($url==null){
echo "get-url-null";exit();
}
$header = array(
'Accept:*/*',
'Accept-Charset:GBK,utf-8;q=0.7,*;q=0.3',
'Accept-Encoding:gzip,deflate,sdch',
'Accept-Language:zh-CN,zh;q=0.8',
'Connection:keep-alive',
);
if($host){
$header = array_merge_recursive($header,array("Host:".$host));
}
else if($this->option["host"]){
$header = array_merge_recursive($header,array("Host:".$this->option["host"]));
}
if($origin){
$header = array_merge_recursive($header,array("Origin:".$origin));
}
else{
$header = array_merge_recursive($header,array("Origin:".$url));
}
if($reffer){
$header = array_merge_recursive($header,array("Referer:".$reffer));
}
else{
$header = array_merge_recursive($header,array("Referer:".$url));
}
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_HTTPHEADER, $header);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 1);
curl_setopt($curl, CURLOPT_USERAGENT, $this->useragent);
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, $location);
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($curl, CURLOPT_AUTOREFERER, 1);
curl_setopt($curl, CURLOPT_ENCODING, "gzip" );
curl_setopt($curl, CURLOPT_HTTPGET, 1);
curl_setopt($curl, CURLOPT_COOKIEJAR, 'cookie.txt');
curl_setopt($curl, CURLOPT_COOKIEFILE, 'cookie.txt');
curl_setopt($curl, CURLOPT_TIMEOUT, 30);
curl_setopt($curl, CURLOPT_HEADER, 1);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
$tmpInfo = curl_exec($curl);
if (curl_errno($curl)) {
echo 'Curl error: '.curl_error ($curl);exit();
}
curl_close($curl);
list($header, $body) = explode("\r\n\r\n", $tmpInfo, 2);
$tmpInfo = $this->auto_charest($tmpInfo);
return array("header"=>$header,"body"=>$body,"content"=>$tmpInfo);
}