此腳本在獲取google.com時正常工作,但未在google.com/search?q=test中正常工作。當我不使用CURLOPT_FOLLOWLOCATION時,我得到302 Moved。當我使用它時,我得到一個頁面要求我輸入驗證碼。我已經嘗試了幾種不同的基於美國的代理,並且改變了用戶代理字符串。有什麼我在這裏失蹤?在使用CURL和PHP通過代理獲取Google搜索結果時遇到問題
function my_fetch($url,$proxy,$user_agent='Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.0.8) Gecko/2009032609 Firefox/3.0.8')
{
$ch = curl_init();
curl_setopt ($ch, CURLOPT_URL, $url);
curl_setopt ($ch, CURLOPT_PROXY, $proxy);
curl_setopt ($ch, CURLOPT_USERAGENT, $user_agent);
curl_setopt ($ch, CURLOPT_HEADER, 0);
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt ($ch, CURLOPT_REFERER, 'http://www.google.com/');
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
curl_setopt ($ch, CURLOPT_TIMEOUT, 20);
$result = curl_exec ($ch);
curl_close ($ch);
return $result;
}
$url = 'http://www.google.com/search?q=test';
$proxy = '152.26.53.4:80';
echo my_fetch($url,$proxy);
請不要回應建議使用API。該API不足以滿足我的需求。
感謝羅傑。我不知道有可能限制Curl訪問。有沒有其他方式可以在不使用API的情況下訪問Google? – 2012-02-03 19:45:59