Home  >  Article  >  Backend Development  >  PHP implementation code of Google PR value

PHP implementation code of Google PR value

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


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn