2012-02-05 39 views
-1

我試圖以遵循這一谷歌重定向使用捲曲,但我不能檢索最終(終端)網址:如何關注Google的「我感到幸運」重定向網址?

http://www.google.com/webhp#q=stackoverflow&btnI

誰能幫助?

我使用的是從PHP網站捲曲功能:

function get_final_url($url, $timeout = 5) 
{ 
$url = str_replace("&", "&", urldecode(trim($url))); 

$cookie = tempnam ("/tmp", "CURLCOOKIE"); 
$ch = curl_init(); 
curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows; U; Windows NT 5.1; rv:1.7.3) Gecko/20041001 Firefox/0.10.1"); 
curl_setopt($ch, CURLOPT_URL, $url); 
curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie); 
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); 
curl_setopt($ch, CURLOPT_ENCODING, ""); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 
curl_setopt($ch, CURLOPT_AUTOREFERER, true); 
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout); 
curl_setopt($ch, CURLOPT_TIMEOUT, $timeout); 
curl_setopt($ch, CURLOPT_MAXREDIRS, 10); 
$content = curl_exec($ch); 
$response = curl_getinfo($ch); 
curl_close ($ch); 

if ($response['http_code'] == 301 || $response['http_code'] == 302) 
{ 
    ini_set("user_agent", "Mozilla/5.0 (Windows; U; Windows NT 5.1; rv:1.7.3)  Gecko/20041001 Firefox/0.10.1"); 
    $headers = get_headers($response['url']); 

    $location = ""; 
    foreach($headers as $value) 
    { 
     if (substr(strtolower($value), 0, 9) == "location:") 
      return get_final_url(trim(substr($value, 9, strlen($value)))); 
    } 
} 

if ( preg_match("/window\.location\.replace\('(.*)'\)/i", $content, $value) || 
     preg_match("/window\.location\=\"(.*)\"/i", $content, $value) 
) 
{ 
    return get_final_url ($value[1]); 
} 
else 
{ 
    return $response['url']; 
} 
} 

echo get_final_url("http://www.google.com/webhp#q=stackoverflow&btnI"); 

是我得到的結果(這也是輸入):

http://www.google.com/webhp#q=stackoverflow&btnI

回答

3

嘗試另一個鏈接:

http://www.google.com/search?q=stackoverflow&btnI= 

這將返回302響應。

+0

好奇它對我來說如何與'http://www.google.com/search?q= stringByReplacingOccurrencesOfString&btnI ='(apple obj-c api docs)不一樣。我認爲這總是第一結果,但也許有一些內部的「自信」var ass-well -_- – vike 2017-05-07 21:55:08

相關問題