i漂泊

 找回密码
 立即注册
搜索
热搜: 活动 交友 discuz
查看: 3949|回复: 0

PHP获取Google PR值 方法一

[复制链接]
TA的礼物信息
  • 收到:0
  • 送出:2
发表于 2013-3-18 20:48:11 | 显示全部楼层 |阅读模式
这是获取Google PR值的PHP实现版本,经过测试运行正常。将下面代码保存为googlepr.php则读取http://www.51piaobo.com/网站的PR值调用格式为:
googlepr.php?url=www.okbase.net
  1. <?php

  2. //PageRank Lookup v1.1 by HM2K (update: 31/01/07)
  3. //based on an alogoritham found here: http://pagerank.gamesaga.net/

  4. //settings - host and user agent
  5. $googlehost='toolbarqueries.google.com';
  6. $googleua='Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.0.6) Gecko/20060728 Firefox/1.5';

  7. //字符串转换到32位整数
  8. function StrToNum($Str, $Check, $Magic) {
  9.     $Int32Unit = 4294967296;  // 2^32

  10.     $length = strlen($Str);
  11.     for ($i = 0; $i < $length; $i++) {
  12.         $Check *= $Magic;   
  13.         //If the float is beyond the boundaries of integer (usually +/- 2.15e+9 = 2^31),
  14.         //  the result of converting to integer is undefined
  15.         //  refer to http://www.php.net/manual/en/language.types.integer.php
  16.         if ($Check >= $Int32Unit) {
  17.             $Check = ($Check - $Int32Unit * (int) ($Check / $Int32Unit));
  18.             //if the check less than -2^31
  19.             $Check = ($Check < -2147483648) ? ($Check + $Int32Unit) : $Check;
  20.         }
  21.         $Check += ord($Str{$i});
  22.     }
  23.     return $Check;
  24. }

  25. //将URL进行哈希编码
  26. function HashURL($String) {
  27.     $Check1 = StrToNum($String, 0x1505, 0x21);
  28.     $Check2 = StrToNum($String, 0, 0x1003F);

  29.     $Check1 >>= 2;   
  30.     $Check1 = (($Check1 >> 4) & 0x3FFFFC0 ) | ($Check1 & 0x3F);
  31.     $Check1 = (($Check1 >> 4) & 0x3FFC00 ) | ($Check1 & 0x3FF);
  32.     $Check1 = (($Check1 >> 4) & 0x3C000 ) | ($Check1 & 0x3FFF);   
  33.      
  34.     $T1 = (((($Check1 & 0x3C0) << 4) | ($Check1 & 0x3C)) <<2 ) | ($Check2 & 0xF0F );
  35.     $T2 = (((($Check1 & 0xFFFFC000) << 4) | ($Check1 & 0x3C00)) << 0xA) | ($Check2 & 0xF0F0000 );
  36.      
  37.     return ($T1 | $T2);
  38. }

  39. //为哈希字符串生成校验码
  40. function CheckHash($Hashnum) {
  41.     $CheckByte = 0;
  42.     $Flag = 0;

  43.     $HashStr = sprintf('%u', $Hashnum) ;
  44.     $length = strlen($HashStr);
  45.      
  46.     for ($i = $length - 1;  $i >= 0;  $i --) {
  47.         $Re = $HashStr{$i};
  48.         if (1 === ($Flag % 2)) {              
  49.             $Re += $Re;     
  50.             $Re = (int)($Re / 10) + ($Re % 10);
  51.         }
  52.         $CheckByte += $Re;
  53.         $Flag ++;   
  54.     }

  55.     $CheckByte %= 10;
  56.     if (0 !== $CheckByte) {
  57.         $CheckByte = 10 - $CheckByte;
  58.         if (1 === ($Flag % 2) ) {
  59.             if (1 === ($CheckByte % 2)) {
  60.                 $CheckByte += 9;
  61.             }
  62.             $CheckByte >>= 1;
  63.         }
  64.     }

  65.     return '7'.$CheckByte.$HashStr;
  66. }

  67. //返回pagerank哈希校验码
  68. function getch($url) { return CheckHash(HashURL($url)); }

  69. //返回PR值
  70. function getpr($url) {
  71.     global $googlehost,$googleua;
  72.     $ch = getch($url);
  73.     $fp = fsockopen($googlehost, 80, $errno, $errstr, 30);
  74.     if ($fp) {
  75.        $out = "GET /search?client=navclient-auto&ch=$ch&features=Rank&q=info:$url HTTP/1.1\r\n";
  76.        //echo "<pre>$out</pre>\n"; //debug only
  77.        $out .= "User-Agent: $googleua\r\n";
  78.        $out .= "Host: $googlehost\r\n";
  79.        $out .= "Connection: Close\r\n\r\n";
  80.      
  81.        fwrite($fp, $out);
  82.         
  83.        //$pagerank = substr(fgets($fp, 128), 4); //debug only
  84.        //echo $pagerank; //debug only
  85.        while (!feof($fp)) {
  86.             $data = fgets($fp, 128);
  87.             //echo $data;
  88.             $pos = strpos($data, "Rank_");
  89.             if($pos === false){} else{
  90.                 $pr=substr($data, $pos + 9);
  91.                 $pr=trim($pr);
  92.                 $pr=str_replace("\n",'',$pr);
  93.                 return $pr;
  94.             }
  95.        }
  96.        //else { echo "$errstr ($errno)
  97. \n"; } //debug only
  98.        fclose($fp);
  99.     }
  100. }

  101. //生成pagerank图形
  102. function pagerank($url,$width=40,$method='style') {
  103.     if (!preg_match('/^(http:\/\/)?([^\/]+)/i', $url)) { $url='http://'.$url; }
  104.     $pr=getpr($url);
  105.     $pagerank="PageRank: $pr/10";

  106.     //The (old) image method
  107.     if ($method == 'image') {
  108.     $prpos=$width*$pr/10;
  109.     $prneg=$width-$prpos;
  110.     $html='<img src="http://www.google.com/images/pos.gif" width='.$prpos.' height=4 border=0 alt="'.$pagerank.'"><img src="http://www.google.com/images/neg.gif" width='.$prneg.' height=4 border=0 alt="'.$pagerank.'">';
  111.     }
  112.     //The pre-styled method
  113.     if ($method == 'style') {
  114.     $prpercent=100*$pr/10;
  115.     $html='<div style="position: relative; width: '.$width.'px; padding: 0; background: #D9D9D9;"><strong style="width: '.$prpercent.'%; display: block; position: relative; background: #5EAA5E; text-align: center; color: #333; height: 4px; line-height: 4px;"><span></span></strong></div>';
  116.     }
  117.      
  118.     $out='<a href="'.$url.'" title="'.$pagerank.'">'.$html.'</a>';
  119.     return $out;
  120. }

  121. if ((!isset($_POST['url'])) && (!isset($_GET['url']))) { echo '<form action="" method="post"><input name="url" type="text"><input type="submit" name="Submit" value="Get Pagerank"></form>'; }
  122. if (isset($_REQUEST['url'])) { echo pagerank($_REQUEST['url']); }
  123. ?>
复制代码
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|小黑屋|手机版|IPiaoBo Inc. ( 渝ICP备17002826号 )

GMT+8, 2024-4-29 14:08 , Processed in 0.074974 second(s), 47 queries .

Powered by Discuz! X3.4

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表