Friday, 10 August 2018

Google Last Cached Date Finder In PHP

When Google looks at a page it takes a snapshot of that page and uses this to match against the query a user entered. To view these cached pages run a Google search and look at the Cached link next to the green URL text of the result. When you view the cached page Google will also give you a date that the page was last cached on. This can be used as a metric of your sites importance as the more often the site is cached, the more favourable Google views your page.
Taking a reading of this metric can therefore be useful, which is why I set about to create a class to retrieve this result.
The class works by constructing a query string with a given URL, which it then uses to download the cached page from Google. It then detects if a date string has been found and if so returns this. If anything went wrong along the way then the function will return false. In an effort to stop Google getting angry with you for trying to spam their site with multiple requests the class will also randomly select from a series of addresses. There is only one address (a single IP address) at the moment, but to add more simply add them to the $googleAddresses array at the top of the class. Every time the getLastCached() function is run a new address will be randomly selected. If you enter an address that causes an error then the function will return false.
Use the class in the following way:
  1. $googleCache = new GoogleCache();
  2. $date = $googleCache->getLastCached("http://www.bbc.co.uk/");
This will return the date when the BBC home page was last cached, which should be very recent.

0 comments:

Post a Comment