2011-10-14 38 views
5

我有以下函數來獲取Googlebot的最後訪問日期:我怎樣才能獲得Google bot上次訪問該頁面的時間?

//get googlebot last access 
function googlebot_lastaccess($domain_name) 
{ 
    $request = 'http://webcache.googleusercontent.com/search?hl=en&q=cache:'.$domain_name.'&btnG=Google+Search&meta='; 
    $data = getPageData($request); 
    $spl=explode("as it appeared on",$data); 
    //echo "<pre>".$spl[0]."</pre>"; 
    $spl2=explode(".<br>",$spl[1]); 
    $value=trim($spl2[0]); 
    //echo "<pre>".$spl2[0]."</pre>"; 
    if(strlen($value)==0) 
    { 
     return(0); 
    } 
    else 
    { 
     return($value); 
    }  
} 

echo "Googlebot last access = ".googlebot_lastaccess($domain_name)."<br />"; 

function getPageData($url) { 
if(function_exists('curl_init')) { 
$ch = curl_init($url); // initialize curl with given url 
curl_setopt($ch, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']); // add useragent 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); // write the response to a variable 
if((ini_get('open_basedir') == '') && (ini_get('safe_mode') == 'Off')) { 
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); // follow redirects if any 
} 
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5); // max. seconds to execute 
curl_setopt($ch, CURLOPT_FAILONERROR, 1); // stop when it encounters an error 
return @curl_exec($ch); 
} 
else { 
return @file_get_contents($url); 
} 
} 

但這個腳本打印我作爲導致整個頁面的屏幕,即快照。整個頁面緩存在谷歌,但我只想捕獲文字as it appeared on後的日期時間,並打印它即:8 Oct 2011 14:03:12 GMT

如何?

+0

我不知道是否有來自谷歌的任何API來獲取值... – Raptor

回答

5

改變這一行:

echo "Googlebot last access = ".googlebot_lastaccess($domain_name)."<br />"; 

與此:

$content = googlebot_lastaccess($domain_name); 
$date = substr($content , 0, strpos($content, 'GMT') + strlen('GMT')); 
echo "Googlebot last access = ".$date."<br />"; 
+0

感謝迅速的回答和工作 – grigione

+0

MMM問題谷歌禁止我我們的系統檢測到您的計算機的異常流量來自網絡。任何軟解決方案? – grigione

+0

@grigione通常他們會禁止你一段時間。不要濫用Google做太多請求。它的使用許可被禁止。 –

3

爲什麼查詢谷歌何時上次在您的網站時,你可以在自己的網站上偵測到Googlebot和什麼網頁的?它還將允許您跟蹤Googlebot在數據庫功能中的簡單寫入情況。

見堆棧溢出問題how to detect search engine bots with php?

相關問題