Calculate Google Pagerank with PHP

This is modified code that I found from the Internet the orginal code that may not work on some server that php has 32bit operation problem and I already tested on my server.

PHP:
  1. <?php
  2. define('GOOGLE_MAGIC', 0xE6359A60);
  3.  
  4. //unsigned shift right
  5. function zeroFill($a, $b)
  6. {
  7.     $z = hexdec(80000000);
  8.         if ($z & $a)
  9.         {
  10.             $a = ($a>>1);
  11.             $a &= (~$z);
  12.             $a |= 0x40000000;
  13.             $a = ($a>>($b-1));
  14.         }
  15.         else
  16.         {
  17.             $a = ($a>>$b);
  18.         }
  19.         return $a;
  20. } 
  21.  
  22. function toInt32(& $x){
  23.   $z = hexdec(80000000);
  24.   $y = (int)$x;
  25.  if($y==-$z&&$x<-$z){
  26.    $y = (int)((-1)*$x);
  27.    $y = (-1)*$y;
  28.   }
  29.   $x = $y;
  30. }
  31.  
  32. function mix($a,$b,$c) {
  33. $a -= $b; $a -= $c; toInt32($a); $a = (int)($a ^ (zeroFill($c,13)));
  34. $b -= $c; $b -= $a; toInt32($b); $b = (int)($b ^ ($a<&lt;8));
  35. $c -= $a; $c -= $b; toInt32($c); $c = (int)($c ^ (zeroFill($b,13)));
  36. $a -= $b; $a -= $c; toInt32($a); $a = (int)($a ^ (zeroFill($c,12)));
  37. $b -= $c; $b -= $a; toInt32($b); $b = (int)($b ^ ($a<&lt;16));
  38. $c -= $a; $c -= $b; toInt32($c); $c = (int)($c ^ (zeroFill($b,5)));
  39. $a -= $b; $a -= $c; toInt32($a); $a = (int)($a ^ (zeroFill($c,3)));
  40. $b -= $c; $b -= $a; toInt32($b); $b = (int)($b ^ ($a<&lt;10));
  41. $c -= $a; $c -= $b; toInt32($c); $c = (int)($c ^ (zeroFill($b,15)));
  42. return array($a,$b,$c);
  43. }
  44.  
  45. function GoogleCH($url, $length=null, $init=GOOGLE_MAGIC) {
  46.     if(is_null($length)) {
  47.         $length = sizeof($url);
  48.     }
  49.     $a = $b = 0x9E3779B9;
  50.     $c = $init;
  51.     $k = 0;
  52.     $len = $length;
  53.     while($len>= 12) {
  54.         $a += ($url[$k+0] +($url[$k+1]<&lt;8) +($url[$k+2]<&lt;16) +($url[$k+3]<&lt;24));
  55.         $b += ($url[$k+4] +($url[$k+5]<&lt;8) +($url[$k+6]<&lt;16) +($url[$k+7]<&lt;24));
  56.         $c += ($url[$k+8] +($url[$k+9]<&lt;8) +($url[$k+10]<&lt;16)+($url[$k+11]<&lt;24));
  57.         $mix = mix($a,$b,$c);
  58.         $a = $mix[0]; $b = $mix[1]; $c = $mix[2];
  59.         $k += 12
  60.         $len -= 12;
  61.     }
  62.  
  63.     $c += $length;
  64.     switch($len)              /* all the case statements fall through */
  65.     {
  66.         case 11: $c+=($url[$k+10]<&lt;24);
  67.         case 10: $c+=($url[$k+9]<&lt;16);
  68.         case 9 : $c+=($url[$k+8]<&lt;8);
  69.           /* the first byte of c is reserved for the length */
  70.         case 8 : $b+=($url[$k+7]<&lt;24);
  71.         case 7 : $b+=($url[$k+6]<&lt;16);
  72.         case 6 : $b+=($url[$k+5]<&lt;8);
  73.         case 5 : $b+=($url[$k+4]);
  74.         case 4 : $a+=($url[$k+3]<&lt;24);
  75.         case 3 : $a+=($url[$k+2]<&lt;16);
  76.         case 2 : $a+=($url[$k+1]<&lt;8);
  77.         case 1 : $a+=($url[$k+0]);
  78.          /* case 0: nothing left to add */
  79.     }
  80.     $mix = mix($a,$b,$c);
  81.     /*-------------------------------------------- report the result */
  82.     return $mix[2];
  83. }
  84.  
  85. //converts a string into an array of integers containing the numeric value of the char
  86. function strord($string) {
  87.     for($i=0;$i<strlen($string);$i++) {
  88.         $result[$i] = ord($string{$i});
  89.     }
  90.     return $result;
  91. }
  92.  
  93.  
  94. // converts an array of 32 bit integers into an array with 8 bit values. Equivalent to (BYTE *)arr32
  95.  
  96. function c32to8bit($arr32) {
  97.     for($i=0;$i<count($arr32);$i++) {
  98.         for ($bitOrder=$i*4;$bitOrder<=$i*4+3;$bitOrder++) {
  99.             $arr8[$bitOrder]=$arr32[$i]&255;
  100.             $arr32[$i]=zeroFill($arr32[$i], 8);
  101.         }     
  102.     }
  103.     return $arr8;
  104. }
  105.  
  106.  
  107. function get_page_rank($url){
  108.     $url = preg_replace('/\?.*$/','?',$url);
  109.     $reqgr = "info:".$url;
  110.     $reqgre = "info:".urlencode($url);     
  111.     $gch = "6".GoogleCH(strord($reqgr));     
  112.         $fsock = fsockopen('toolbarqueries.google.com', 80, $errno, $errstr);
  113.         if ( !$fsock ){   
  114.             echo 'Can not connect to server';
  115.             return -1;
  116.         }
  117.         $base_get = "/search?client=navclient-auto&ch=".$gch."&ie=UTF-8&oe=UTF-8&features=Rank:FVN&q=".$reqgre;
  118.         fputs($fsock, "GET $base_get HTTP/1.1\r\n");
  119.         fputs($fsock, "HOST: toolbarqueries.google.com\r\n");
  120.         fputs($fsock, "User-Agent: Mozilla/4.0 (compatible; GoogleToolbar 2.0.114-big; Windows XP 5.1)\r\n");
  121.         fputs($fsock, "Connection: close\r\n\r\n");
  122.         while(!feof($fsock)){ 
  123.             $res['content'] .= fread($fsock, 1024);
  124.         }
  125.         fclose($fsock);
  126.         if(preg_match('/Rank_.*?:.*?:(\d+)/i', $res['content'], $m)){     
  127.             return $m[1];
  128.         }else{     
  129.             return -1;
  130.         }       
  131. }
  132.  
  133. echo get_page_rank("www.google.com");
  134.  
  135. ?>



Leave a Reply

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <code> <em> <i> <strike> <strong>