/* PHP & MySQL Blog */
Mark Woodman has created a nifty class which lets you query Google pagerank info in PHP 5 and above. The complete classes are available here.
The minimum classes required are ‘cacher.class.php‘ and ‘google_pagerank.class.php‘.
Sample code is shown below.
require_once("popstats/google_pagerank.class.php"); $rankObject = new GooglePageRank("http://www.someDomain.com"); $pageRank = $rankObject->pagerank; echo $pageRank; |
The class also supports result caching on the local machine, so if a second query arrives within the cache time limit the result saved in the local file is returned rather then going out and querying Google.
The ‘GooglePageRank’ class accepts cache time as a second parameter. So if you want to cache the result for 6 hours you set the second parameter for 21600 seconds (6 * 60 * 60):
$rankObject = new GooglePageRank("http://www.someDomain.com", 21600); |
The default is set to 24 hours, which is more then adequate for most purposes.
|
|
This site is a digital habitat of Sameer, a freelance web developer working from Pune.More
9 Responses
1
Weekend Links - Firefox 3 Hacks, jQuery Kwicks, Shorthand CSS, XMPPHP, Google Page Rank PHP
April 20th, 2008 at 7:51 am
[...] http://www.codediesel.com/php/google-pagerank-in-php/ addthis_url = ‘http%3A%2F%2Fdavidwalsh.name%2Ffirefox-3-hacks-jquery-kwicks-shorthand-css-xmpphp-page-rank-php’; addthis_title = ‘Weekend+Links+-+Firefox+3+Hacks%2C+jQuery+Kwicks%2C+Shorthand+CSS%2C+XMPPHP%2C+Google+Page+Rank+PHP’; addthis_pub = ”; [...]
2
PHP Weekly Reader - April 20th 2008 : phpaddiction
April 23rd, 2008 at 11:22 pm
[...] Google RageRank in PHP — I can’t think of anything I would use this for, but somebody somewhere is obsessing over their PR and needs this. [...]
3
arnel aritcheta
June 12th, 2008 at 2:27 am
is there a code for php 4
sameer
June 12th, 2008 at 2:34 am
what exact version of PHP 4 are you asking.
5
Webagentur
July 4th, 2008 at 3:26 am
Does the script? I’ve had a few that did not work.
sameer
July 4th, 2008 at 3:40 am
It works. For e.g it gives a page rank of ‘4′ for ’smashingmagazine.com’.
7
CoOokie
July 29th, 2008 at 10:22 am
Thanks for this little piece of code, it’s nice base for my script
8
gipson
May 26th, 2009 at 1:38 am
This topic is a little old, but files are still there so I’d like to suggest something:
In line 50 of google_pagerank.class.php file you used ‘urlencode’ function. It works until there is ‘&’ in link string, otherwise ‘&’ is treated as another Google’s parameter. Changing ‘urlencode’ to ‘rawurlencode’ solves the problem.
Nice work btw!
P.S. Another little suggestion: try adding cURL (if unable to file_get_contents) and various Google Server (and get it random when checking - that way you won’t have to do ‘usleep’ being afraid of ban from Google).
9
Alex
June 14th, 2009 at 6:07 am
hey,
Have you solved it already? i want to use it on my website:
http://www.ozrot.co.il