Google Pagerank Script in PHP (Updated June 2012)

Important: This script has been updated as of May 2012, so the script runs on 64bit operating systems.

Recently, I’ve been searching for a PHP script that was able to retrieve the pagerank of a specific web page, since Google doesn’t provide an API for it. I found many options, although none of them worked since Google has changed their pagerank hash, so therefore, I have decided to dig through the Google Toolbar source code, and find out on my own.

<?php

function genhash ($url) {
	$hash = "Mining PageRank is AGAINST GOOGLE'S TERMS OF SERVICE. Yes, I'm talking to you, scammer.";
	$c = 16909125;
	$length = strlen($url);
	$hashpieces = str_split($hash);
	$urlpieces = str_split($url);
	for ($d = 0; $d < $length; $d++) {
		$c = $c ^ (ord($hashpieces[$d]) ^ ord($urlpieces[$d]));
		$c = (($c >> 23) & 0x1ff) | $c << 9;
 	}
 	$c = -(~($c & 4294967295) + 1);
 	return '8' . dechex($c);
}

function pagerank($url) {
	$googleurl = 'http://toolbarqueries.google.com/tbr?client=navclient-auto&ch=' . genhash($url) . '&features=Rank&q=info:' . urlencode($url);
	if(function_exists('curl_init')) {
		$ch = curl_init();
		curl_setopt($ch, CURLOPT_HEADER, 0);
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
		curl_setopt($ch, CURLOPT_URL, $googleurl);
		$out = curl_exec($ch);
		curl_close($ch);
	} else {
		$out = file_get_contents($googleurl);
	}
	if(strlen($out) > 0) {
		return trim(substr(strrchr($out, ':'), 1));
	} else {
		return -1;
	}
}

echo pagerank('http://www.fusionswift.com/');
?>

You may have been brought here from the October 2011 version of the script. While the original date on this post is from older back, this is the “more” working version. The October 2011 version works, though only on 32 bit operating systems.

35 thoughts on “Google Pagerank Script in PHP (Updated June 2012)

  1. Realizzazione Siti Web Bologna

    Hello,
    I am experimenting with some scripts I have found online in order to get the Google’s PageRank of a specified page.
    All the scripts, including yours, seem not to work.

    Whatever URL you try, the pagerank is (blank) and if I try the api:
    http://api.fusionswift.com/pagerank/?url=http://fusionswift.com/

    it will show the same numbers regardless the URL specified. I guess it’s not working.

    It seems that in 2007 the PageRank check was very simple to perform, by means of PHP4 and some SOAP script.
    There was a Google Api which could be used – nowadays at Google they seem to have wiped away this API. There are many APIs to interact with most Google services, but not Web Search: I can only find a Ajax API (working!) but it not yelds any PageRank data… Just good for plain searches.

    Anybody can shed some light on this?

    1. tech163 Post author

      Our API is in fact having some issues at the moment. However, our pagerank() function should still be working.

  2. Anonymous

    we used the above code but the page rank is blank..what is the problem? what we have to do further?

  3. edevlet

    is there any limitation for daily pagerank check?
    As I remember google is selling pagerank check.
    But the first 1000 is free.
    Anybody knows something about this?
    Thanks in advance

    1. tech163 Post author

      I’m not sure what Google’s policy is. I don’t think Google officially sells pagerank check. I haven’t seen anything like that around.

    1. Tech163 Post author

      Are you under a 64 bit operating system? Unfortunately this only works under 32 bit at the moment.

Comments are closed.