Source for file pagerankPhp.php

Documentation is available at pagerankPhp.php

  1. <?php
  2. /**
  3.  * Google Page rank data grabber.
  4.  * Based on Google toolbar data.
  5.  * 
  6.  * Working with data like that:
  7.  * "Rank_1:1:6". Without XML/HTML parsing.
  8.  * 
  9.  * @important:
  10.  *  This code is for cognitive purposes only.
  11.  *  Usage of this code is against Google's terms of service.
  12.  *  No support is provided for this code.
  13.  *  Can be used "as is" for your own risk.
  14.  * 
  15.  * @requirements:
  16.  *  - PHP version 5 (can easily be downgraded to PHP4)
  17.  *  - cURL library (but you can replace curl functions with
  18.  *  PHP standard fopen/fread or other)
  19.  * 
  20.  * @version 1.1
  21.  * @author exstabler // PHP version
  22.  * @author snoopy    // JS version
  23.  * 
  24.  * @changelog:
  25.  *  - 2007-01-18:
  26.  *  1. [exstabler] Deprecated method intToHex deleted.
  27.  *  2. [exstabler] Method toHex8 corrected.
  28.  * 
  29.  * @sample usage:
  30.  *  $obj = new PageRankGrabber();
  31.  *  $rank = $obj->getRank('http://dir.org.ru');
  32.  */
  33. class PageRankGrabber {
  34.     
  35.     /**
  36.      * Returns PageRank for specified page
  37.      * @param string $url 
  38.      * @return integer PageRank value
  39.      */
  40.     public function getRank($url{
  41.         $pageUrl $this->getRankUrlStingByPageUrl($url);
  42.         $content $this->getContentCurl($pageUrl);
  43.         $parts   explode(":"$content);
  44.         return intval(@$parts[count($parts)-1]);
  45.     }
  46.     
  47.     /**
  48.      * Returns content by URL with using
  49.      * cURL library.
  50.      *
  51.      * @param string $url 
  52.      * @return string Site content
  53.      */
  54.     public function getContentCurl($url{
  55.        $ch curl_init();
  56.        curl_setopt($chCURLOPT_URL$url);
  57.        curl_setopt($chCURLOPT_HEADER0);
  58.        curl_setopt($chCURLOPT_RETURNTRANSFER1);
  59.        curl_setopt($chCURLOPT_FOLLOWLOCATION1);
  60.        $result curl_exec($ch);
  61.        curl_close($ch);
  62.        return $result;        
  63.     }
  64.     
  65.     /**
  66.      * Returning URL where PR can be grabbed.
  67.      *
  68.      * @param string $page - Page URL
  69.      * @return string 
  70.      */
  71.     public function getRankUrlStingByPageUrl($page{
  72.         $this->awesomeHash($page);
  73.         $hash "8" $this->awesomeHash($page);
  74.         $url  "http://toolbarqueries.google.com/search?sourceid=" .
  75.                 "navclient-ff&features=Rank&client=navclient-auto-ff&";
  76.         $url .= "ch=" $hash "&q=info:" urlencode($page);
  77.         return str_replace(' '''$url);        
  78.     }
  79.         
  80.     /**
  81.      * Transforms integer into hexademical
  82.      *
  83.      * @param int $num 
  84.      * @return string 
  85.      */
  86.     private function toHex8($num{
  87.         $vector "0123456789abcdef";
  88.         return $vector[($num 25616$vector[$num 16];
  89.     }
  90.     
  91.     /**
  92.      * Service function: encoder
  93.      */
  94.     private function hexEncodeU32($num{
  95.         $result  $this->toHex8($this->zerofillShift($num24));
  96.         $result .= $this->toHex8($this->zerofillShift($num16255);
  97.         $result .= $this->toHex8($this->zerofillShift($num8255);
  98.         return $result $this->toHex8($num 255);
  99.     }
  100.  
  101.     /**
  102.      * Service function: hashing
  103.      */
  104.     private function awesomeHash($value)  {
  105.  
  106.         $hashSeed "Mining PageRank is AGAINST GOOGLE'S TERMS OF SERVICE." .
  107.                     "Yes, I'm talking to you, scammer.";
  108.         $intValue 16909125;
  109.         for($i 0$i strlen($value)$i++ ){
  110.             $intValue ^=
  111.                 $this->charCodeAt($hashSeed$i strlen($hashSeed)) ^
  112.                 $this->charCodeAt($value$i);
  113.             $intValue =
  114.                 $this->zerofillShift($intValue,  23$intValue << 9;
  115.         }
  116.         return $this->hexEncodeU32($intValue);
  117.     }
  118.     
  119.     /**
  120.      * The charCodeAt() method returns the Unicode
  121.      * of the character at a specified position.
  122.      *
  123.      * @param int $value 
  124.      */
  125.     private function charCodeAt($value$position{
  126.         $symbol $value[$position];
  127.         // ord() is for ASCII!
  128.         // Original function should work with UTF-8.
  129.         return ord($symbol);
  130.     }
  131.  
  132.     /**
  133.      * Service function: zerofil with shifing
  134.      * (unsigned shift right).
  135.      */
  136.     private function zerofillShift($a$b{
  137.         $z hexdec(80000000);
  138.         if ($z $a{
  139.             $a ($a >> 1);
  140.             $a &= (~$z);
  141.             $a |= 0x40000000;
  142.             $a ($a >> ($b-1));
  143.         else {
  144.             $a ($a >> $b);
  145.         }
  146.         return $a;
  147.     }   
  148. }

Documentation generated on Thu, 18 Jan 2007 03:54:04 +0000 by phpDocumentor 1.3.1