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.


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 = '' . 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);
	} else {
		$out = file_get_contents($googleurl);
	if(strlen($out) > 0) {
		return trim(substr(strrchr($out, ':'), 1));
	} else {
		return -1;

echo pagerank('');

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. Marco Kellershoff

    Hello :)

    Is there any limitation using your API? We would like to use your api once a week requesting about 120k to 150k pageranks. If this would not be possible due performance issues we would love to obtain/buy the code you used to receive the pagerank from google.

    Kind regards


    1. tech163 Post author

      @Marco Kellershoff, performance shouldn’t be an issue if you’re requesting that many pageranks, unless if you’re creating several thousand simultaneous connections. HOWEVER, Google might have a limit on the amount of pageranks allowed by each IP address.

  2. Brendon

    Thanks for this. The reason it doesn’t worth on some servers is probably due to 32-bit/64-bit issues; I had a similar problem writing a script for short links. It’s worth pointing out that while Google doesn’t provide an API, there are APIs available.

    1. tech163 Post author

      @Brendon, I understand what you’re saying. The script doesn’t work on my MacBook, and doesn’t work on some other computers either. However, I have created a Google PageRank API myself.


    I just want to say thank you thank you and thank you. Your code works!
    I just hope you won’t remove you API :)

  4. Alfons Gunawan

    I want to know: if I used your API, for my web. It’s like domain gallery, where the page ranks are shown automatically, is there any probability I get error results?

    FYI, my web will request hundreds until thousands page ranks everyday.

    Thank you very very much.

  5. wim

    Thank you very much. I changed servers last week and I noticed the PR bar didn’t work anymore. Is it possible to have an API like this for the verious data-centers of Google? Thanks again, Wim

  6. Anonymous

    Thanks a lot, I was searching for this for 4 hours but all I could find was the PHP code from 404, working only with version 1.1.

    Thanks again, keep up!

Comments are closed.