首页  >  文章  >  后端开发  >  Google PR值的PHP实现代码

Google PR值的PHP实现代码

WBOY
WBOY原创
2016-07-25 08:57:491169浏览
  1. //PageRank Lookup v1.1 by HM2K (update: 31/01/07)
  2. //based on an alogoritham found here: http://pagerank.gamesaga.net/
  3. //settings - host and user agent
  4. $googlehost='toolbarqueries.google.com';
  5. $googleua='Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.0.6) Gecko/20060728 Firefox/1.5';
  6. //字符串转换到32位整数
  7. function StrToNum($Str, $Check, $Magic) {
  8. $Int32Unit = 4294967296; // 2^32
  9. $length = strlen($Str);
  10. for ($i = 0; $i $Check *= $Magic;
  11. //If the float is beyond the boundaries of integer (usually +/- 2.15e+9 = 2^31),
  12. // the result of converting to integer is undefined
  13. // refer to http://www.php.net/manual/en/language.types.integer.php
  14. if ($Check >= $Int32Unit) {
  15. $Check = ($Check - $Int32Unit * (int) ($Check / $Int32Unit));
  16. //if the check less than -2^31
  17. $Check = ($Check }
  18. $Check += ord($Str{$i});
  19. }
  20. return $Check;
  21. }
  22. //将URL进行哈希编码
  23. function HashURL($String) {
  24. $Check1 = StrToNum($String, 0x1505, 0x21);
  25. $Check2 = StrToNum($String, 0, 0x1003F);
  26. $Check1 >>= 2;
  27. $Check1 = (($Check1 >> 4) & 0x3FFFFC0 ) | ($Check1 & 0x3F);
  28. $Check1 = (($Check1 >> 4) & 0x3FFC00 ) | ($Check1 & 0x3FF);
  29. $Check1 = (($Check1 >> 4) & 0x3C000 ) | ($Check1 & 0x3FFF);
  30. $T1 = (((($Check1 & 0x3C0) $T2 = (((($Check1 & 0xFFFFC000)
  31. return ($T1 | $T2);
  32. }
  33. //为哈希字符串生成校验码
  34. function CheckHash($Hashnum) {
  35. $CheckByte = 0;
  36. $Flag = 0;
  37. $HashStr = sprintf('%u', $Hashnum) ;
  38. $length = strlen($HashStr);
  39. for ($i = $length - 1; $i >= 0; $i --) {
  40. $Re = $HashStr{$i};
  41. if (1 === ($Flag % 2)) {
  42. $Re += $Re;
  43. $Re = (int)($Re / 10) + ($Re % 10);
  44. }
  45. $CheckByte += $Re;
  46. $Flag ++;
  47. }
  48. $CheckByte %= 10;
  49. if (0 !== $CheckByte) {
  50. $CheckByte = 10 - $CheckByte;
  51. if (1 === ($Flag % 2) ) {
  52. if (1 === ($CheckByte % 2)) {
  53. $CheckByte += 9;
  54. }
  55. $CheckByte >>= 1;
  56. }
  57. }
  58. return '7'.$CheckByte.$HashStr;
  59. }
  60. //返回pagerank哈希校验码
  61. function getch($url) { return CheckHash(HashURL($url)); }
  62. //返回PR值
  63. function getpr($url) {
  64. global $googlehost,$googleua;
  65. $ch = getch($url);
  66. $fp = fsockopen($googlehost, 80, $errno, $errstr, 30);
  67. if ($fp) {
  68. $out = "GET /search?client=navclient-auto&ch=$ch&features=Rank&q=info:$url HTTP/1.1\r\n";
  69. //echo "
    $out
    \n"; //debug only
  70. $out .= "User-Agent: $googleua\r\n";
  71. $out .= "Host: $googlehost\r\n";
  72. $out .= "Connection: Close\r\n\r\n";
  73. fwrite($fp, $out);
  74. //$pagerank = substr(fgets($fp, 128), 4); //debug only
  75. //echo $pagerank; //debug only
  76. while (!feof($fp)) {
  77. $data = fgets($fp, 128);
  78. //echo $data;
  79. $pos = strpos($data, "Rank_");
  80. if($pos === false){} else{
  81. $pr=substr($data, $pos + 9);
  82. $pr=trim($pr);
  83. $pr=str_replace("\n",'',$pr);
  84. return $pr;
  85. }
  86. }
  87. //else { echo "$errstr ($errno)
    \n"; } //debug only
  88. fclose($fp);
  89. }
  90. }
  91. //生成pagerank图形
  92. function pagerank($url,$width=40,$method='style') {
  93. if (!preg_match('/^(http:\/\/)?([^\/]+)/i', $url)) { $url='http://'.$url; }
  94. $pr=getpr($url);
  95. $pagerank="PageRank: $pr/10";
  96. //The (old) image method
  97. if ($method == 'image') {
  98. $prpos=$width*$pr/10;
  99. $prneg=$width-$prpos;
  100. $html=''.$pagerank.''.$pagerank.'';
  101. }
  102. //The pre-styled method
  103. if ($method == 'style') {
  104. $prpercent=100*$pr/10;
  105. $html='
    ';
  106. }
  107. $out=''.$html.'';
  108. return $out;
  109. }
  110. if ((!isset($_POST['url'])) && (!isset($_GET['url']))) { echo '
    '; }
  111. if (isset($_REQUEST['url'])) { echo pagerank($_REQUEST['url']); }
  112. ?>
复制代码


声明:
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn