Google "Last Cached" Date Script

Discussion in 'PHP' started by robster, Mar 20, 2007.

  1. #1
    Hi,

    I am trying to find a way to get the last cached date from google in a simple script.

    Does anyone know of a script that does this? I know that there has got to be something out there because I have seen plugins that fetch this date, such as SEO firefox extensions.

    Thanks for any help you may be able to give!!
     
    robster, Mar 20, 2007 IP
  2. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #2
    I wrote this quick. It requires cURL to be installed and enabled.

    
    function fetch_google_last_cache($url)
    {
    	$ch = curl_init("http://www.google.com/search?q=info:{$url}");
    	curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    
    	if (preg_match('/http:\/\/[\d]{1,3}\.[\d]{1,3}\.[\d]{1,3}\.[\d]{1,3}\/search\?q=cache[^"]+/', curl_exec($ch), $cache_url))
    	{
    		curl_setopt($ch, CURLOPT_URL, html_entity_decode($cache_url[0]));
    		
    		if (preg_match('/as retrieved on ([^<]+)/', curl_exec($ch), $date))
    		{
    			return trim($date[1]);
    		}
    	}
    	
    	return false;
    }
    
    
    
    PHP:
    Usage example.
    
    $date = fetch_google_last_cache('http://forums.digitalpoint.com');
    
    if ($date === false)
    {
         echo 'Your site does not seem to be cached by Google.';
    }
    else
    {
         echo 'This site was last cached on: '. $date;
    }
    
    
    PHP:
     
    nico_swd, Mar 20, 2007 IP