[請益]curl抓網頁拿不到cookie

看板PHP作者 (TEST123)時間13年前 (2012/12/13 18:31), 編輯推噓0(000)
留言0則, 0人參與, 最新討論串1/1
最近要抓一個頁面 卻不能全部抓下來 觀察之後發現是要有cookie才可以看到的頁面 我參考了別人的文章寫了一個function 可是卻抓不到cookie 不知道我是不是缺了什麼參數? <?php $url = 'http://s.taobao.com/search?q=1'; $res = get_page($url); echo $res; function get_page($url){ $user_agent = 'Mozilla/5.0 (Windows NT 6.1;WOW64) AppleWebKit/537.11 (KHTML, like Gecko) Chrome/23.0.1271.95 Safari/537.11'; $cookie_fn = './taobao_cookie.txt'; if(!file_exists($cookie_fn)){ output('', $cookie_fn, 'w'); } $ch = curl_init(); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_HEADER, true);//false true curl_setopt($ch, CURLOPT_USERAGENT, $user_agent); curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie_fn); curl_setopt($ch, CURLOPT_REFERER, $url); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2); $ch = curl_init(); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_HEADER, true); curl_setopt($ch, CURLOPT_USERAGENT, $user_agent); curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie_fn); curl_setopt($ch, CURLOPT_REFERER, $url); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2); $res2 = curl_exec($ch); $res2 = iconv("GBK", "UTF-8", $res2); curl_close($ch); return $res2; } function output($content, $fn, $type){ $fp = fopen($fn, $type); fputs($fp, $content); fclose($fp); } ?> -- -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 114.34.50.241
文章代碼(AID): #1GoQv-k7 (PHP)
文章代碼(AID): #1GoQv-k7 (PHP)