關於的google curl的get返回

用curl抓取google的搜索結果後返回302錯誤,在網上搜了一下,加了cookies後就正常返回html

header('Content-Type: text/html; charset=utf-8'); $cookie_file = dirname(__FILE__).'/cookie.txt'; //先獲取cookies並保存 $url = "http://www.google.co.jp"; $ch = curl_init($url); //初始化 curl_setopt($ch, CURLOPT_HEADER, 0); //不返回header部分 curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); //返回字符串,而非直接輸出 curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie_file); //存儲cookies curl_exec($ch); curl_close($ch); //使用上面保存的cookies再次訪問 $url = "http://www.google.co.jp/search?oe=utf8&ie=utf8&source=uds&hl=zh-CN&q=kkk"; $ch = curl_init($url); curl_setopt($ch, CURLOPT_HEADER, 0); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie_file); //使用上面獲取的cookies $response = curl_exec($ch); curl_close($ch); echo $response;cookie

相關文章
相關標籤/搜索