2013-01-02 28 views
1

我試圖執行一個cURL請求(直接從shell或通過PHP),將返回一個URL的內容基本上相同的請求通過瀏覽器(減去任何cookie /登錄等)。爲什麼cURL請求會返回國際頁面(例如Google搜索)?

www.google.com的基本cURL請求將返回似乎是日語版Google搜索的某些字符編碼問題。

測試與選項,包括設置一個標準的用戶代理,並按照位置仍不會導致什麼,我認爲將是一個非常類似的請求我的瀏覽器。是否有一組標誌我應該用來嚴格模仿瀏覽器請求?

下面的代碼是目前用於測試,但即使被存儲的cookie谷歌假定位置是日本(google.co.jp)。冰壺當

$header = array(
     "Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8", 
     "Accept-Language: en-us,en;q=0.5", 
     "Connection: keep-alive", 
     "Cache-Control: no-cache", 
     "Content-Type: application/x-www-form-urlencoded; charset=UTF-8", 
     "Pragma: no-cache", 
    ); 
$useragent = 'Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; .NET CLR 1.1.4322)'; 

$ch = curl_init(); 
curl_setopt($ch, CURLOPT_VERBOSE, 0); 
curl_setopt($ch, CURLOPT_URL, $request); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); 
curl_setopt($ch, CURLOPT_HTTPHEADER, $header); 
curl_setopt($ch, CURLOPT_HEADER, 0); 
curl_setopt($ch, CURLOPT_COOKIEJAR, "my_cookies.txt"); 
curl_setopt($ch, CURLOPT_COOKIEFILE, "my_cookies.txt"); 
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); 
curl_setopt($ch, CURLOPT_AUTOREFERER, true); 
curl_setopt($ch, CURLOPT_TIMEOUT, 10); 
$data = curl_exec($ch); 
curl_close($ch); 
+0

可能是缺乏餅乾?也許谷歌存儲一些餅乾您的PC上知道你是從什麼地方,併爲您提供從這個 – Lock

+0

正確的谷歌網站發佈,請您正在使用的代碼。 –

+0

我已經包含在問題我目前的測試代碼,感謝@cryptic – Rhys

回答

2
$header = array(
     "Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8", 
     "Accept-Language: en-us,en;q=0.5", 
     "Connection: keep-alive", 
     "Cache-Control: no-cache", 
     "Content-Type: application/x-www-form-urlencoded; charset=UTF-8", 
     "Pragma: no-cache", 
    ); 
$useragent = 'Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; .NET CLR 1.1.4322)'; 

$ch = curl_init(); 
curl_setopt($ch, CURLOPT_VERBOSE, 0); 
curl_setopt($ch, CURLOPT_URL, $request); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); 
curl_setopt($ch, CURLOPT_HTTPHEADER, $header); 
curl_setopt($ch, CURLOPT_HEADER, 0); 
curl_setopt($ch, CURLOPT_COOKIEJAR, "my_cookies.txt"); 
curl_setopt($ch, CURLOPT_COOKIEFILE, "my_cookies.txt"); 
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); 
curl_setopt($ch, CURLOPT_AUTOREFERER, true); 
curl_setopt($ch, CURLOPT_TIMEOUT, 10); 
curl_setopt($ch, CURLOPT_PROXY, 'PROXY_IP_HERE:PROXY_PORT'); // Use a proxy located in USA 
$data = curl_exec($ch); 
curl_close($ch); 
相關問題