PHP Functions untuk Query Google PageRank

Posted on April 5, 2008 
Filed Under PHP

Sumber: PHP PageRank query script

Berikut functions-nya, sudah saya coba dan bekerja dengan sempurna.

Cara pakai:
$pr = getpr(”http://example.com/dir/anu/ini.html”);
echo $pr;

  1. <?php
  2. // 3/20/2008 - Updated by Roger Collins (http://www.rogercollins.com/)
  3. // to remove graphing step
  4.  
  5. //PageRank Lookup v1.1 by HM2K (update: 31/01/07)
  6. //based on an alogoritham found here: http://pagerank.gamesaga.net/
  7.  
  8. //settings - host and user agent
  9. $googlehost='toolbarqueries.google.com';
  10. $googleua='Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.0.6) Gecko/20060728 Firefox/1.5';
  11.  
  12. //convert a string to a 32-bit integer
  13. function StrToNum($Str, $Check, $Magic) {
  14.     $Int32Unit = 4294967296// 2^32
  15.  
  16.     $length = strlen($Str);
  17.     for ($i = 0; $i < $length; $i++) {
  18.         $Check *= $Magic;     
  19.         //If the float is beyond the boundaries of integer (usually +/- 2.15e+9 = 2^31),
  20.         //  the result of converting to integer is undefined
  21.         //  refer to http://www.php.net/manual/en/language.types.integer.php
  22.         if ($Check >= $Int32Unit) {
  23.             $Check = ($Check - $Int32Unit * (int) ($Check / $Int32Unit));
  24.             //if the check less than -2^31
  25.             $Check = ($Check < -2147483648) ? ($Check + $Int32Unit) : $Check;
  26.         }
  27.         $Check += ord($Str{$i});
  28.     }
  29.     return $Check;
  30. }
  31.  
  32. //genearate a hash for a url
  33. function HashURL($String) {
  34.     $Check1 = StrToNum($String, 0x1505, 0x21);
  35.     $Check2 = StrToNum($String, 0, 0x1003F);
  36.  
  37.     $Check1 >>= 2;     
  38.     $Check1 = (($Check1 >> 4) & 0x3FFFFC0 ) | ($Check1 & 0x3F);
  39.     $Check1 = (($Check1 >> 4) & 0x3FFC00 ) | ($Check1 & 0x3FF);
  40.     $Check1 = (($Check1 >> 4) & 0x3C000 ) | ($Check1 & 0x3FFF);   
  41.    
  42.     $T1 = (((($Check1 & 0x3C0) << 4) | ($Check1 & 0x3C)) <<2 ) | ($Check2 & 0xF0F );
  43.     $T2 = (((($Check1 & 0xFFFFC000) << 4) | ($Check1 & 0x3C00)) << 0xA) | ($Check2 & 0xF0F0000 );
  44.    
  45.     return ($T1 | $T2);
  46. }
  47.  
  48. //genearate a checksum for the hash string
  49. function CheckHash($Hashnum) {
  50.     $CheckByte = 0;
  51.     $Flag = 0;
  52.  
  53.     $HashStr = sprintf('%u', $Hashnum) ;
  54.     $length = strlen($HashStr);
  55.    
  56.     for ($i = $length - 1$i >= 0$i --) {
  57.         $Re = $HashStr{$i};
  58.         if (1 === ($Flag % 2)) {             
  59.             $Re += $Re;     
  60.             $Re = (int)($Re / 10) + ($Re % 10);
  61.         }
  62.         $CheckByte += $Re;
  63.         $Flag ++;   
  64.     }
  65.  
  66.     $CheckByte %= 10;
  67.     if (0 !== $CheckByte) {
  68.         $CheckByte = 10 - $CheckByte;
  69.         if (1 === ($Flag % 2) ) {
  70.             if (1 === ($CheckByte % 2)) {
  71.                 $CheckByte += 9;
  72.             }
  73.             $CheckByte >>= 1;
  74.         }
  75.     }
  76.  
  77.     return '7'.$CheckByte.$HashStr;
  78. }
  79.  
  80. //return the pagerank checksum hash
  81. function getch($url) { return CheckHash(HashURL($url)); }
  82.  
  83. //return the pagerank figure
  84. function getpr($url) {
  85.     global $googlehost,$googleua;
  86.     $pr = 0;    // default return
  87.     $ch = getch($url);
  88.     $fp = fsockopen($googlehost, 80, $errno, $errstr, 30);
  89.     if ($fp) {
  90.        $out = "GET /search?client=navclient-auto&ch=$ch&features=Rank&q=info:$url HTTP/1.1\r\n";
  91.        //echo "<pre>$out</pre>\n"; //debug only
  92.        $out .= "User-Agent: $googleua\r\n";
  93.        $out .= "Host: $googlehost\r\n";
  94.        $out .= "Connection: Close\r\n\r\n";
  95.    
  96.        fwrite($fp, $out);
  97.        
  98.        //$pagerank = substr(fgets($fp, 128), 4); //debug only
  99.        //echo $pagerank; //debug only
  100.        while (!feof($fp)) {
  101.             $data = fgets($fp, 128);
  102.             //echo $data;
  103.             $pos = strpos($data, "Rank_");
  104.             if($pos === false){} else{
  105.                 $pr=substr($data, $pos + 9);
  106.                 $pr=trim($pr);
  107.                 $pr=str_replace("\n",'',$pr);
  108.                 return $pr;
  109.             }
  110.        }
  111.        //else { echo "$errstr ($errno)<br />\n"; } //debug only
  112.        fclose($fp);
  113.     }
  114.     return $pr;
  115. }
  116.  
  117. ?>

Comments

4 Responses to “PHP Functions untuk Query Google PageRank”

  1. Fujitsu on April 7th, 2008 5:08 am

    Pertanyaan kemaren kena akismet kayaknya, nanya lagi ah.

    cara pakenya masih kurang jelas om, bisa lebih rinci lagi? Thank you.

  2. ngkong on April 7th, 2008 10:51 am

    @atas
    tokocomputer.info tapi kok jualannya jeans? :D

    caranya, code di dalam kotak tinggal di copas ke notepad trus di save misalnya dg nama pagerank.php. lalu dari file lain tinggal include file tersebut misal include(”pagerank.php”); kemudian pakai getpr($url); yang akan return PR url tsb.

    mas fujitsu klo paling ngga ngerti dikit2 php pasti paham maksudnya…

  3. Fujitsu on April 8th, 2008 9:13 am

    He he he he he, maklum om nama juga newbi :D habis copas dari toko satunya lom di edit he he he.

    tetap lom jelas om, buta PHP solae hiks hiks.

    Thank you ya.

  4. widis on May 27th, 2008 10:48 pm

    Thanks, lumayan nih, lumayan bikin pusing he he he :D

Leave a Reply