Php script Google Pagerank

Some web builders are looking for php script with Google Pagerank value of page.

Script updated 13.10.2011 – This is actualy working version.

We have easy code in PHP with GPL license. Use it in PHP code $pagerank=getGooglePageRank(“http://www.domain.com/example.html”); and variable $pagerank will be filled by number 0-10 or will be null, when pagerank is not set for this url.

Just copy and use it for free.

<?
function strToNum($str,$check,$magic){
   $int32_unit = 4294967296;  // 2^32
   $length = strlen($str);
   for($i=0;$i<$length;$i++){
      $check *= $magic;
      if($check >= $int32_unit){
         $check -= $int32_unit * (int)($check/$int32_unit);
         //if the check less than -2^31
         if($check < -2147483648)
         $check += $int32_unit;
      }
      $check += ord($str{$i});
   }
   return $check;
}

function hashUrl($str){
   $check1 = strToNum($str, 0x1505, 0x21);
   $check2 = strToNum($str, 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);
}

function checkHash($hash_num){
   $check_byte = 0;
   $flag = 0;
   $hash_str = sprintf('%u', $hash_num) ;
   $length = strlen($hash_str);
   for($i=($length-1);$i>=0;$i--){
      $re = $hash_str{$i};
      if(1 === ($flag % 2)){
         $re += $re;
         $re = (int)($re / 10) + ($re % 10);
      }
      $check_byte += $re;
      $flag++;
   }
   $check_byte %= 10;
   if(0 !== $check_byte){
      $check_byte = 10 - $check_byte;
      if(1 === ($flag % 2)){
         if(1 === ($check_byte % 2))
         $check_byte += 9;
         $check_byte >>= 1;
      }
   }
   return '7'.$check_byte.$hash_str;
}

function getGooglePageRank($url){
   $google_host = 'toolbarqueries.google.com';
   $google_user_agent = 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.0.6) Gecko/20060728 Firefox/1.5';

   $ch = checkHash(hashUrl($url));
   $fp = fsockopen($google_host, 80, $errno, $errstr, 30);
   if ($fp){
      $out = "GET /tbr?client=navclient-auto&ch=$ch&features=Rank&q=info:$url HTTP/1.1\r\n";
      $out.="User-Agent: $google_user_agent\r\nHost: $google_host\r\nConnection: Close\r\n\r\n";
      fwrite($fp, $out);
      while (!feof($fp)){
         $data = fgets($fp, 128);
         $pos = strpos($data, "Rank_");
         if($pos === false){
         }else{
            $pr = substr($data, $pos + 9);
            $pr = trim($pr);
            $pr = str_replace("\n",'',$pr);
            return $pr;
         }
      }
      fclose($fp);
   }
}

print(getGooglePageRank('http://www.seotest.com'));

?>

2 Responses to "Php script Google Pagerank"

TOPlist