源碼以下:php
1 <?php 2 header("Content-Type:text/html;charset=utf-8"); 3 //模擬羣友通信錄手機號登陸 4 $curl = curl_init(); 5 $cookie_jar = tempnam('./tmp','cookie'); //tempnam--創建一個具備惟一文件名的文件 6 curl_setopt($curl, CURLOPT_URL,'http://qun.hk/oauth/login');//這裏寫上處理登陸的界面 7 curl_setopt($curl, CURLOPT_POST, 1); //post提交數據 8 $request = 'username=***&password=***'; 9 curl_setopt($curl, CURLOPT_POSTFIELDS, $request);//傳遞數據 10 curl_setopt($curl, CURLOPT_COOKIEJAR, $cookie_jar);//把返回來的cookie信息保存在$cookie_jar文件中 11 curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);//設定返回的數據是否自動顯示 12 curl_setopt($curl, CURLOPT_HEADER, false);//設定是否顯示頭信息 13 curl_setopt($curl, CURLOPT_NOBODY, false);//設定是否輸出頁面內容 14 curl_exec($curl);//返回結果 15 curl_close($curl); //關閉*/ 16 ?>
執行程序可把登陸獲取的cookie信息保存到文件中html
如下是運用cookie文件的例子:json
1 <?php 2 header("Content-Type:text/html;charset=utf-8"); 3 $cookie_jar = dirname(__FILE__)."/tmp/coo4B58.tmp"; //保存的cookie信息文件路徑 4 $url="http://qun.hk/index/view/code/0c248f6bfa13/page/3"; 5 $arr=curl_get($url,$cookie_jar); 6 var_dump($arr); 7 8 /** 9 *curl post請求 10 */ 11 function curl_post($url,$data,$cookie_jar){ 12 $ch=curl_init(); 13 curl_setopt($ch, CURLOPT_URL, $url); 14 curl_setopt ( $ch, CURLOPT_POST, 1 ); 15 curl_setopt ( $ch, CURLOPT_HEADER, 0 ); 16 curl_setopt ( $ch, CURLOPT_RETURNTRANSFER, 1 ); 17 curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie_jar); 18 curl_setopt ( $ch, CURLOPT_POSTFIELDS, $data ); 19 $return = curl_exec ( $ch ); 20 curl_close ( $ch ); 21 } 22 23 /** 24 *curl get請求 25 */ 26 function curl_get($url,$cookie_jar){ 27 $curl2 = curl_init(); 28 curl_setopt($curl2, CURLOPT_URL, $url);//登錄後要從哪一個頁面獲取信息 29 curl_setopt($curl2, CURLOPT_HEADER, false); 30 curl_setopt($curl2, CURLOPT_RETURNTRANSFER, 1); 31 curl_setopt($curl2, CURLOPT_COOKIEFILE, $cookie_jar); 32 $content = curl_exec($curl2); 33 $arr=json_decode($content,true); 34 curl_close($curl2); 35 return $arr; 36 }