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.

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

  1. lenjerie intima

    I’ve been looking for several days such a script, i hope it will work, because i have already tested some others, but becuase of the Google’s changes in calculating and displaying algorithm.
    Thank so much for this, i will comeback to confirm working of the above PHP code!

    Reply
  2. Igor

    I tried it.
    Here is what I got:
    “Your client does not have permission to get URL /tbr?client=navclient-auto&ch=878a41377&features=Rank&q=info:http%3A%2F%2Fwww.tech163.info%2F from this server.”

    So it doesnt work for me.. Any ideas why?

    Reply
    1. Tech163 Post author

      Weird. The May 2012 version seemed to have some minor problems with certain URLs, yet worked with others. I’ve updated the script again (June 2012) so it should work with all the other URLs.

  3. Patrik

    the code is working, thank you!
    Impossible to know how many requests can be made ‚Äč‚Äčevery day, the limit of 100 or 1000?

    Reply
  4. Vigneshraj.n

    Hi.. I am trying to create php app for page rank in gtk.. I hope this works.. Thanks for sharing the script

    Reply

Leave a Reply

Your email address will not be published.