Skip to content

linux和windows下均可查询pr的php代码

转载本站文章请注明:
转载来至:[记录与PHP的PK经历]
本文链接: linux和windows下均可查询pr的php代码

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

";
 102:     
 103:        fwrite($fp, $out);
 104:        
 105:        //$pagerank = substr(fgets($fp, 128), 4); //debug only
 106:        //echo $pagerank; //debug only
 107:        while (!feof($fp)) {
 108:             $data = fgets($fp, 128);
 109:             //echo $data;
 110:             $pos = strpos($data, "Rank_");
 111:             if($pos === false){} else{
 112:                 $pr=substr($data, $pos + 9);
 113:                 $pr=trim($pr);
 114:                 $pr=str_replace("
",'',$pr);
 115:                 return $pr;
 116:             }
 117:        }
 118:        //else { echo "$errstr ($errno)<br />
"; } //debug only
 119:        fclose($fp);
 120:     }
 121:     return $pr;
 122:     }
 123: }
 124: //$gpr = new PageRank();
 125: //print_r($gpr->printrank("http://www.pkphp.com/"));


<?

One Comment

  1. 测试下

    Posted on 25-Jul-08 at 10:36 am | Permalink

One Trackback/Pingback

  1. Google PageRank for WordPress on 30-Jul-08 at 7:36 pm

    [...] askie 给我的一个查询页面 PR 值的类,做出了一个WordPress 插件,它能够获取每篇日志的 PR [...]

Post a Comment

Your email is never published nor shared. Required fields are marked *
*
*