Geekpedia Programming Tutorials






Retrieve PageRank using PHP

This PHP code is designed to work on servers running the latest version of PHP as well, unlike most of the code samples on the web which will not work on many servers because of a checksum error.

On Tuesday, January 1st 2008 at 05:44 PM
By Andrew Pociu (View Profile)
****-   (Rated 4 with 4 votes)
Contextual Ads
More PHP Resources
Advertisement
  1. <?php
  2. //PageRank Lookup v1.1 by HM2K (update: 31/01/07)
  3. //based on an alogoritham found here: http://pagerank.gamesaga.net/
  4.  
  5. //settings - host and user agent
  6. $googlehost='toolbarqueries.google.com';
  7. $googleua='Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.0.6) Gecko/20060728 Firefox/1.5';
  8.  
  9. //convert a string to a 32-bit integer
  10. function StrToNum($Str, $Check, $Magic) {
  11.     $Int32Unit = 4294967296;  // 2^32
  12.  
  13.     $length = strlen($Str);
  14.     for ($i = 0; $i < $length; $i++) {
  15.         $Check *= $Magic;      
  16.         //If the float is beyond the boundaries of integer (usually +/- 2.15e+9 = 2^31),
  17.         //  the result of converting to integer is undefined
  18.         //  refer to http://www.php.net/manual/en/language.types.integer.php
  19.         if ($Check >= $Int32Unit) {
  20.             $Check = ($Check - $Int32Unit * (int) ($Check / $Int32Unit));
  21.             //if the check less than -2^31
  22.             $Check = ($Check < -2147483648) ? ($Check + $Int32Unit) : $Check;
  23.         }
  24.         $Check += ord($Str{$i});
  25.     }
  26.     return $Check;
  27. }
  28.  
  29. //genearate a hash for a url
  30. function HashURL($String) {
  31.     $Check1 = StrToNum($String, 0x1505, 0x21);
  32.     $Check2 = StrToNum($String, 0, 0x1003F);
  33.  
  34.     $Check1 >>= 2;      
  35.     $Check1 = (($Check1 >> 4) & 0x3FFFFC0 ) | ($Check1 & 0x3F);
  36.     $Check1 = (($Check1 >> 4) & 0x3FFC00 ) | ($Check1 & 0x3FF);
  37.     $Check1 = (($Check1 >> 4) & 0x3C000 ) | ($Check1 & 0x3FFF)
  38.        
  39.     $T1 = (((($Check1 & 0x3C0) << 4) | ($Check1 & 0x3C)) <<2 ) | ($Check2 & 0xF0F );
  40.     $T2 = (((($Check1 & 0xFFFFC000) << 4) | ($Check1 & 0x3C00)) << 0xA) | ($Check2 & 0xF0F0000 );
  41.        
  42.     return ($T1 | $T2);
  43. }
  44.  
  45. //genearate a checksum for the hash string
  46. function CheckHash($Hashnum) {
  47.     $CheckByte = 0;
  48.     $Flag = 0;
  49.  
  50.     $HashStr = sprintf('%u', $Hashnum) ;
  51.     $length = strlen($HashStr);
  52.        
  53.     for ($i = $length - 1;  $i >= 0;  $i --) {
  54.         $Re = $HashStr{$i};
  55.         if (1 === ($Flag % 2)) {              
  56.             $Re += $Re;    
  57.             $Re = (int)($Re / 10) + ($Re % 10);
  58.         }
  59.         $CheckByte += $Re;
  60.         $Flag ++;       
  61.     }
  62.  
  63.     $CheckByte %= 10;
  64.     if (0 !== $CheckByte) {
  65.         $CheckByte = 10 - $CheckByte;
  66.         if (1 === ($Flag % 2) ) {
  67.             if (1 === ($CheckByte % 2)) {
  68.                 $CheckByte += 9;
  69.             }
  70.             $CheckByte >>= 1;
  71.         }
  72.     }
  73.  
  74.     return '7'.$CheckByte.$HashStr;
  75. }
  76.  
  77. //return the pagerank checksum hash
  78. function getch($url) { return CheckHash(HashURL($url)); }
  79.  
  80. //return the pagerank figure
  81. function getpr($url) {
  82.         global $googlehost,$googleua;
  83.         $ch = getch($url);
  84.         $fp = fsockopen($googlehost, 80, $errno, $errstr, 30);
  85.         if ($fp) {
  86.            $out = "GET /search?client=navclient-auto&ch=$ch&features=Rank&q=info:$url HTTP/1.1\r\n";
  87.            //echo "<pre>$out</pre>\n"; //debug only
  88.            $out .= "User-Agent: $googleua\r\n";
  89.            $out .= "Host: $googlehost\r\n";
  90.            $out .= "Connection: Close\r\n\r\n";
  91.        
  92.            fwrite($fp, $out);
  93.            
  94.            //$pagerank = substr(fgets($fp, 128), 4); //debug only
  95.            //echo $pagerank; //debug only
  96.            while (!feof($fp)) {
  97.                         $data = fgets($fp, 128);
  98.                         //echo $data;
  99.                         $pos = strpos($data, "Rank_");
  100.                         if($pos === false){} else{
  101.                                 $pr=substr($data, $pos + 9);
  102.                                 $pr=trim($pr);
  103.                                 $pr=str_replace("\n",'',$pr);
  104.                                 return $pr;
  105.                         }
  106.            }
  107.            //else { echo "$errstr ($errno)<br />\n"; } //debug only
  108.            fclose($fp);
  109.         }
  110. }
  111.  
  112. //generate the graphical pagerank
  113. function pagerank($url,$width=40,$method='style') {
  114.         if (!preg_match('/^(http:\/\/)?([^\/]+)/i', $url)) { $url='http://'.$url; }
  115.         $pr=getpr($url);
  116.         $pagerank="PageRank: $pr/10";
  117.  
  118.         //The (old) image method
  119.         if ($method == 'image') {
  120.         $prpos=$width*$pr/10;
  121.         $prneg=$width-$prpos;
  122.         $html='<img src="http://www.google.com/images/pos.gif" width='.$prpos.' height=4 border=0 alt="'.$pagerank.'"><img src="http://www.google.com/images/neg.gif" width='.$prneg.' height=4 border=0 alt="'.$pagerank.'">';
  123.         }
  124.         //The pre-styled method
  125.         if ($method == 'style') {
  126.         $prpercent=100*$pr/10;
  127.         $html='<div style="position: relative; width: '.$width.'px; padding: 0; background: #D9D9D9;"><strong style="width: '.$prpercent.'%; display: block; position: relative; background: #5EAA5E; text-align: center; color: #333; height: 4px; line-height: 4px;"><span></span></strong></div>';
  128.         }
  129.        
  130.         $out='<a href="'.$url.'" title="'.$pagerank.'">'.$html.'</a>';
  131.         return $out;
  132. }
  133.  
  134. //if ((!isset($_POST['url'])) && (!isset($_GET['url']))) { echo '<form action="" method="post"><input name="url" type="text"><input type="submit" name="Submit" value="Get Pagerank"></form>'; }
  135. if (isset($_REQUEST['url'])) { echo pagerank($_REQUEST['url']); }
  136. ?>
Digg Digg It!     Del.icio.us Del.icio.us     Reddit Reddit     StumbleUpon StumbleIt     Newsvine Newsvine     Furl Furl     BlinkList BlinkList

Rate Rate this code snippet
Comment Current Comments
by Arif Budiman on Saturday, February 23rd 2008 at 12:57 AM

Pretty good code for get PageRank from google, how ever when i am running why the result is \"PageRank: /10\", can you explain to me ?
and Thanx for the code

by semaj on Friday, August 7th 2009 at 03:55 AM

because the variable which is "$pr" $pagerank="PageRank: $pr/10"; is not concatenated to the string which is stored to the $pagerank variable.....hope you can get it right now...cheers!!!

by Dan on Sunday, November 8th 2009 at 02:35 AM

Works for me, thanks!

by Ken on Saturday, January 30th 2010 at 04:10 PM

Hi Andrew (or anybody),
I've use the above script for a while and it works most of the time. But there are some URL's for which it wont work such as

www.familyfunonadime.com

Any idea why some URL's wont work?

Thanks,

Ken


Comment Comment on this tutorial
Name: Email:
Message:
Comment Related Source Code
There is no related code.

Comment Related Tutorials
There are no related tutorials.

Jobs PHP Job Search
My skills include:

Enter a City:

Select a State:


Advanced Search >>
Advertisement

Free Magazine Subscriptions

Today's Pictures

Today's Video

Other Resources

Latest Download

Latest Icons