由於近段時間幫朋友開發一個能夠查詢正方教務系統的微信公眾平台號。有所收獲。這里總結下個人經驗。
開講前,先吐槽一下新浪雲服務器,一個程序里的 同一個函數 在PC測試可以正常運行,在它那里就會掛的現象。
老樣子,我將在代碼里注釋清楚。使用下面的函數,將會獲得兩種形式的 cookie,一種保存在文件中,一種直接以變量的形式返回,
經驗提示: 有時候,在不同的代碼運行環境中,帶着文件cookie 去訪問會成功,而變量卻失敗,有時候卻想法。不過,
目前,這兩種方法總有一種會成功。
1 function get_cookie($url_,$params_,$referer_){ 2 3 if($url_==null){echo "get_cookie_url_null";exit;} 4 if($params_==null){echo "get_params_null";exit;} 5 if($referer_==null){echo "get_referer-null";exit;} 6 $this_header = array("content-type: application/x-www-form-urlencoded; charset=UTF-8");//訪問鏈接時要發送的頭信息 7 8 $ch = curl_init($url_);//這里是初始化一個訪問對話,並且傳入url,這要個必須有 9 10 //curl_setopt就是設置一些選項為以后發起請求服務的 11 12 13 curl_setopt($ch,CURLOPT_HTTPHEADER,$this_header);//一個用來設置HTTP頭字段的數組。使用如下的形式的數組進行設置: array('Content-type: text/plain', 'Content-length: 100') 14 curl_setopt($ch, CURLOPT_HEADER,1);//如果你想把一個頭包含在輸出中,設置這個選項為一個非零值,我這里是要輸出,所以為 1 15 16 curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);//將 curl_exec()獲取的信息以文件流的形式返回,而不是直接輸出。設置為0是直接輸出 17 18 curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);//設置跟蹤頁面的跳轉,有時候你打開一個鏈接,在它內部又會跳到另外一個,就是這樣理解 19 20 curl_setopt($ch,CURLOPT_POST,1);//開啟post數據的功能,這個是為了在訪問鏈接的同時向網頁發送數據,一般數urlencode碼 21 22 curl_setopt($ch,CURLOPT_POSTFIELDS,$params_); //把你要提交的數據放這 23 24 curl_setopt($ch, CURLOPT_COOKIEJAR, 'cookie.txt');//獲取的cookie 保存到指定的 文件路徑,我這里是相對路徑,可以是$變量 25 26 //curl_setopt($ch, CURLOPT_COOKIEFILE, 'cookie.txt');//要發送的cookie文件,注意這里是文件,還一個是變量形式發送 27 28 //curl_setopt($curl, CURLOPT_COOKIE, $this->cookies);//例如這句就是設置以變量的形式發送cookie,注意,這里的cookie變量是要先獲取的,見下面獲取方式 29 30 curl_setopt ($ch, CURLOPT_REFERER,$referer_); //在HTTP請求中包含一個'referer'頭的字符串。告訴服務器我是從哪個頁面鏈接過來的,服務器籍此可以獲得一些信息用於處理。 31 32 $content=curl_exec($ch); //重點來了,上面的眾多設置都是為了這個,進行url訪問,帶着上面的所有設置 33 34 if(curl_errno($ch)){ 35 echo 'Curl error: '.curl_error($ch);exit(); //這里是設置個錯誤信息的反饋 36 } 37 38 if($content==false){ 39 echo "get_content_null";exit(); 40 } 41 preg_match('/Set-Cookie:(.*);/iU',$content,$str); //這里采用正則匹配來獲取cookie並且保存它到變量$str里,這就是為什么上面可以發送cookie變量的原因 42 43 $cookie = $str[1]; //獲得COOKIE(SESSIONID) 44 45 curl_close($ch);//關閉會話 46 47 return $cookie;//返回cookie 48 }
下面這個是如何利用上面的cookie 去訪問網頁,去post數據,去get頁面代碼的函數。
1 function post($url,$post_data,$location = 0,$reffer = null,$origin = null,$host = null){ 2 3 $post_data = is_array($post_data)?http_build_query($post_data):$post_data; 4 //產生一個urlencode之后的請求字符串,因為我們post,傳送給網頁的數據都是經過處理,一般是urlencode編碼后才發送的 5 6 $header = array( //頭部信息,上面的函數已說明 7 'Accept:*/*', 8 'Accept-Charset:text/html,application/xhtml+xml,application/xml;q=0.7,*;q=0.3', 9 'Accept-Encoding:gzip,deflate,sdch', 10 'Accept-Language:zh-CN,zh;q=0.8', 11 'Connection:keep-alive', 12 'Content-Type:application/x-www-form-urlencoded', 13 //'CLIENT-IP:'.$ip, 14 //'X-FORWARDED-FOR:'.$ip, 15 ); 16 17 //下面的都是頭部信息的設置,請根據他們的變量名字,對應上面函數所說明 18 if($host){ 19 $header = array_merge_recursive($header,array("Host:".$host)); 20 } 21 else if($this->option["host"]){ 22 $header = array_merge_recursive($header,array("Host:".$this->option["host"])); 23 } 24 if($origin){ 25 $header = array_merge_recursive($header,array("Origin:".$origin)); 26 } 27 else{ 28 $header = array_merge_recursive($header,array("Origin:".$url)); 29 } 30 if($reffer){ 31 $header = array_merge_recursive($header,array("Referer:".$reffer)); 32 } 33 else{ 34 $header = array_merge_recursive($header,array("Referer:".$url)); 35 } 36 37 $curl = curl_init(); //這里並沒有帶參數初始化 38 39 curl_setopt($curl, CURLOPT_URL, $url);//這里傳入url 40 41 curl_setopt($curl, CURLOPT_HTTPHEADER, $header); 42 43 curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0);//對認證證書來源的檢查,不開啟次功能 44 45 curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 1);//從證書中檢測 SSL 加密算法 46 47 curl_setopt($curl, CURLOPT_USERAGENT, $this->useragent); 48 //模擬用戶使用的瀏覽器,自己設置,我的是"Mozilla/5.0 (Windows NT 6.1; WOW64; rv:23.0) Gecko/20100101 Firefox/23.0" 49 curl_setopt($curl, CURLOPT_FOLLOWLOCATION, $location); 50 51 curl_setopt($curl, CURLOPT_AUTOREFERER, 1);//自動設置referer 52 53 curl_setopt($curl, CURLOPT_POST, 1);//開啟post 54 55 curl_setopt($curl, CURLOPT_ENCODING, "gzip" ); 56 //HTTP請求頭中"Accept-Encoding: "的值。支持的編碼有"identity","deflate"和"gzip"。如果為空字符串"",請求頭會發送所有支持的編碼類型。 57 //我上面設置的是*/* 58 59 curl_setopt($curl, CURLOPT_POSTFIELDS, $post_data);//要傳送的數據 60 61 //curl_setopt($curl, CURLOPT_COOKIE, $this->cookies);//以變量形式發送cookie,我這里沒用它,文件保險點 62 63 curl_setopt($curl, CURLOPT_COOKIEJAR, 'cookie.txt'); //存cookie的文件名, 64 65 curl_setopt($curl, CURLOPT_COOKIEFILE, 'cookie.txt'); //發送 66 67 curl_setopt($curl, CURLOPT_TIMEOUT, 30);//設置超時限制,防止死循環 68 69 curl_setopt($curl, CURLOPT_HEADER, 1); 70 71 curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); 72 73 $tmpInfo = curl_exec($curl); 74 if (curl_errno($curl)) { 75 echo 'Curl error: ' . curl_error ( $curl );exit(); 76 } 77 78 curl_close($curl); 79 list($header, $body) = explode("\r\n\r\n", $tmpInfo, 2);//分割出網頁源代碼的頭和bode 80 $tmpInfo = $this->auto_charest($tmpInfo);//轉碼,防止亂碼,自定義函數 81 return array("header"=>$header,"body"=>$body,"content"=>$tmpInfo); 82 }
上面是post,下面是get,兩者差不多,差別在於,get是沒有post,傳送數據給放前訪問的網頁的,僅僅只是獲取源代碼。
1 function get($url,$location = 1,$origin = null,$reffer = null,$host = null){ 2 //$ip = $this->randip(); 3 if($url==null){ 4 echo "get-url-null";exit(); 5 } 6 $header = array( 7 'Accept:*/*', 8 'Accept-Charset:GBK,utf-8;q=0.7,*;q=0.3', 9 'Accept-Encoding:gzip,deflate,sdch', 10 'Accept-Language:zh-CN,zh;q=0.8', 11 'Connection:keep-alive', 12 13 //'CLIENT-IP:'.$ip, 14 //'X-FORWARDED-FOR:'.$ip, 15 ); 16 if($host){ 17 $header = array_merge_recursive($header,array("Host:".$host)); 18 } 19 else if($this->option["host"]){ 20 $header = array_merge_recursive($header,array("Host:".$this->option["host"])); 21 } 22 if($origin){ 23 $header = array_merge_recursive($header,array("Origin:".$origin)); 24 } 25 else{ 26 $header = array_merge_recursive($header,array("Origin:".$url)); 27 } 28 if($reffer){ 29 $header = array_merge_recursive($header,array("Referer:".$reffer)); 30 } 31 else{ 32 $header = array_merge_recursive($header,array("Referer:".$url)); 33 } 34 35 $curl = curl_init(); 36 curl_setopt($curl, CURLOPT_URL, $url); 37 curl_setopt($curl, CURLOPT_HTTPHEADER, $header); 38 curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0); 39 curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 1); 40 curl_setopt($curl, CURLOPT_USERAGENT, $this->useragent); 41 curl_setopt($curl, CURLOPT_FOLLOWLOCATION, $location); 42 curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1); 43 curl_setopt($curl, CURLOPT_AUTOREFERER, 1); 44 curl_setopt($curl, CURLOPT_ENCODING, "gzip" ); 45 curl_setopt($curl, CURLOPT_HTTPGET, 1); 46 //curl_setopt($curl, CURLOPT_COOKIE, $this->cookies); 47 curl_setopt($curl, CURLOPT_COOKIEJAR, 'cookie.txt'); 48 curl_setopt($curl, CURLOPT_COOKIEFILE, 'cookie.txt'); 49 curl_setopt($curl, CURLOPT_TIMEOUT, 30); 50 curl_setopt($curl, CURLOPT_HEADER, 1); 51 curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); 52 $tmpInfo = curl_exec($curl); 53 54 if (curl_errno($curl)) { 55 echo 'Curl error: '.curl_error ($curl);exit(); 56 } 57 curl_close($curl); 58 list($header, $body) = explode("\r\n\r\n", $tmpInfo, 2); 59 $tmpInfo = $this->auto_charest($tmpInfo); 60 return array("header"=>$header,"body"=>$body,"content"=>$tmpInfo); 61 }
OK,如果覺得對你有點用的,請默默點一下頂。(右下角)