2011-12-22 84 views
0

此腳本在獲取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不足以滿足我的需求。

回答

0

谷歌已不再是cURL。

Google不再通過Curl授予訪問權限,它可能會爲您提供302條移動消息,如果您想使用它,則必須使用它。

感謝

+0

感謝羅傑。我不知道有可能限制Curl訪問。有沒有其他方式可以在不使用API​​的情況下訪問Google? – 2012-02-03 19:45:59

0

你可以嘗試這樣做與PhantomJS:

var page = require("webpage").create(); 
var homePage = "http://www.google.com/"; 

page.open(homePage); 
page.onLoadFinished = function(status) { 
var url = page.url; 

console.log("Status: " + status); 
console.log("Loaded: " + url); 


page.includeJs("http://code.jquery.com/jquery-1.8.3.min.js", function() { 
    console.log("Loaded jQuery!"); 
    page.evaluate(function() { 
    var searchBox = $(".lst"); 
    var searchForm = $("form"); 

    searchBox.val("your query"); 
    searchForm.submit(); 
    }); 
}); 

window.setTimeout(
     function() { 
      page.render('google.png'); 
      phantom.exit(0); 
     }, 
     1000 // wait 5,000ms (5s) 
    ); 


}; 
相關問題