use php to get google page rank
Since the google not give a API or interface to get the google page rank, we have to use google toolbar to get the PR. In order to use php to query toolbarqueries.google.com to the get PR, I did a search ,but most of the codes are too old and can’t use or not work well, at last i got following script works well.
use php to get google page rank
there is all the codes :
<?php function StrToNum($Str, $Check, $Magic) { $Int32Unit = 4294967296; // 2^32 $length = strlen($Str); for ($i = 0; $i < $length; $i++) { $Check *= $Magic; //If the float is beyond the boundaries of integer (usually +/- 2.15e+9 = 2^31), // the result of converting to integer is undefined // refer to http://www.php.net/manual/en/language.types.integer.php if ($Check >= $Int32Unit) { $Check = ($Check - $Int32Unit * (int) ($Check / $Int32Unit)); //if the check less than -2^31 $Check = ($Check < -2147483648) ? ($Check + $Int32Unit) : $Check; } $Check += ord($Str{$i}); } return $Check; } //--> for google pagerank /* * Genearate a hash for a url */ function HashURL($String) { $Check1 = StrToNum($String, 0x1505, 0x21); $Check2 = StrToNum($String, 0, 0x1003F); $Check1 >>= 2; $Check1 = (($Check1 >> 4) & 0x3FFFFC0 ) | ($Check1 & 0x3F); $Check1 = (($Check1 >> 4) & 0x3FFC00 ) | ($Check1 & 0x3FF); $Check1 = (($Check1 >> 4) & 0x3C000 ) | ($Check1 & 0x3FFF); $T1 = (((($Check1 & 0x3C0) << 4) | ($Check1 & 0x3C)) <<2 ) | ($Check2 & 0xF0F ); $T2 = (((($Check1 & 0xFFFFC000) << 4) | ($Check1 & 0x3C00)) << 0xA) | ($Check2 & 0xF0F0000 ); return ($T1 | $T2); } //--> for google pagerank /* * genearate a checksum for the hash string */ function CheckHash($Hashnum) { $CheckByte = 0; $Flag = 0; $HashStr = sprintf('%u', $Hashnum) ; $length = strlen($HashStr); for ($i = $length - 1; $i >= 0; $i --) { $Re = $HashStr{$i}; if (1 === ($Flag % 2)) { $Re += $Re; $Re = (int)($Re / 10) + ($Re % 10); } $CheckByte += $Re; $Flag ++; } $CheckByte %= 10; if (0 !== $CheckByte) { $CheckByte = 10 - $CheckByte; if (1 === ($Flag % 2) ) { if (1 === ($CheckByte % 2)) { $CheckByte += 9; } $CheckByte >>= 1; } } return '7'.$CheckByte.$HashStr; } //get google pagerank function getpagerank($url) { $query="http://toolbarqueries.google.com/search?client=navclient-auto&ch=".CheckHash(HashURL($url)). "&features=Rank&q=info:".$url."&num=100&filter=0"; $data=file_get_contents_curl($query); //print_r($data); $pos = strpos($data, "Rank_"); if($pos === false){} else{ $pagerank = substr($data, $pos + 9); return $pagerank; } } function file_get_contents_curl($url) { $ch = curl_init(); curl_setopt($ch, CURLOPT_HEADER, 0); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); //Set curl to return the data instead of printing it to the browser. curl_setopt($ch, CURLOPT_URL, $url); $data = curl_exec($ch); curl_close($ch); return $data; } ?>
How to use it
Save the previous codes to a pr.php , the test.php
<?php include "pr.php"; $url='http://www.brightyoursite.com'; echo "$url PR is ". getPagerank($url) ; ?>
Source File
live demo
, I'm a freelancer and available now,if need help just contact me without hesitation.
16 Responses to “use php to get google page rank”
Leave a Reply
-
Search for:
-
Sponsor
Categories
- apache (2)
- css xhtml (10)
- html5 (1)
- javascript (3)
- jquery (5)
- magento (3)
- mysql (1)
- php (1)
- SEO Friendly (6)
- Tools (3)
- w3c validation (4)
- Wordpress (4)


















This is a superb post use php to get google page rank.
But I was wondering how do I suscribe to the RSS feed?
if you need such a script just contact me
I鈥檝e recently started a blog, the information you provide on this site has helped me tremendously. Thank you for all of your time & work.
My cousin recommended this blog and she was totally right keep up the fantastic work!
I’m getting a 403 forbidden return when I run this (no pr output). Thoughts?
URL of the script: http://jtgraphic.net/test/pr.php?url=http://www.brightyoursite.com
I’m doing a print_r on data for now.
http://www.brightyoursite.com/demo/pr/test.php the demo it’s works fine in my project not sure whats wrong with you
This is such a great resource that you are providing and you give it away for free. I enjoy seeing websites that understand the value of providing a prime resource for free. I truly loved reading your post. Thanks!
Good fill someone in on and this enter helped me alot in my college assignement. Thanks you as your information.
It was very helpfull thank you so much for sharing. I will share it with my friends. Thanks
thanks!You made some good points there. I did a search on the topic and found most people will agree with your blog
[...] I found one that was fast, efficient and didn’t drain the server’s resources.I found it at brightyoursite.com but since I use CodeIgniter for most of my PHP work I decided to rewrite it to a class and clean it [...]
Thanks, you guys explained everthing I needed to know and very quicly 10 out of 10!
You wrote very interesting post i love your blog it contains very informative article i will share it with my friends.
it was very interesting to read.
I want to quote your post in my blog. It can?
And you et an account on Twitter?
[...] I found one that was fast, efficient and didn’t drain the server’s resources.I found it at brightyoursite.com but since I use CodeIgniter for most of my PHP work I decided to rewrite it to a class and clean it [...]
+1
yes you can but need point out the reference link