首頁  >  文章  >  後端開發  >  php取得網站的google pr值

php取得網站的google pr值

WBOY
WBOY原創
2016-07-25 08:57:501168瀏覽
  1. /**
  2. * php代码取得网站的PR值
  3. * by bbs.it-home.org
  4. */
  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. echo getpr('http://bbs.it-home.org');
  8. //convert a string to a 32-bit integer
  9. function StrToNum($Str, $Check, $Magic) {
  10. $Int32Unit = 4294967296; // 2^32
  11. $length = strlen($Str);
  12. for ($i = 0; $i < $length; $i++) {
  13. $Check *= $Magic;
  14. //If the float is beyond the boundaries of integer (usually +/- 2.15e+9 = 2^31),
  15. // the result of converting to integer is undefined
  16. // refer to http://www.php.net/manual/en/language.types.integer.php
  17. if ($Check >= $Int32Unit) {
  18. $Check = ($Check - $Int32Unit * (int) ($Check / $Int32Unit));
  19. //if the check less than -2^31
  20. $Check = ($Check < -2147483648) ? ($Check + $Int32Unit) : $Check;
  21. }
  22. $Check += ord($Str{$i});
  23. }
  24. return $Check;
  25. }
  26. //genearate a hash for a url
  27. function HashURL($String) {
  28. $Check1 = StrToNum($String, 0×1505, 0×21);
  29. $Check2 = StrToNum($String, 0, 0×1003F);
  30. $Check1 >>= 2;
  31. $Check1 = (($Check1 >> 4) & 0×3FFFFC0 ) | ($Check1 & 0×3F);
  32. $Check1 = (($Check1 >> 4) & 0×3FFC00 ) | ($Check1 & 0×3FF);
  33. $Check1 = (($Check1 >> 4) & 0×3C000 ) | ($Check1 & 0×3FFF);
  34. $T1 = (((($Check1 & 0×3C0) << 4) | ($Check1 & 0×3C)) <<2 ) | ($Check2 & 0xF0F );
  35. $T2 = (((($Check1 & 0xFFFFC000) << 4) | ($Check1 & 0×3C00)) << 0xA) | ($Check2 & 0xF0F0000 );
  36. return ($T1 | $T2);
  37. }
  38. //genearate a checksum for the hash string
  39. function CheckHash($Hashnum) {
  40. $CheckByte = 0;
  41. $Flag = 0;
  42. $HashStr = sprintf(‘%u’, $Hashnum) ;
  43. $length = strlen($HashStr);
  44. for ($i = $length - 1; $i >= 0; $i –) {
  45. $Re = $HashStr{$i};
  46. if (1 === ($Flag % 2)) {
  47. $Re += $Re;
  48. $Re = (int)($Re / 10) + ($Re % 10);
  49. }
  50. $CheckByte += $Re;
  51. $Flag ++;
  52. }
  53. $CheckByte %= 10;
  54. if (0 !== $CheckByte) {
  55. $CheckByte = 10 - $CheckByte;
  56. if (1 === ($Flag % 2) ) {
  57. if (1 === ($CheckByte % 2)) {
  58. $CheckByte += 9;
  59. }
  60. $CheckByte >>= 1;
  61. }
  62. }
  63. return "7".$CheckByte.$HashStr;
  64. }
  65. //return the pagerank checksum hash
  66. function getch($url) { return CheckHash(HashURL($url)); }
  67. //return the pagerank figure
  68. function getpr($url) {
  69. global $googlehost,$googleua;
  70. $pr = 0; // default return
  71. $ch = getch($url);
  72. $fp = fsockopen($googlehost, 80, $errno, $errstr, 30);
  73. if ($fp) {
  74. $out = "GET /search?client=navclient-auto&ch=$ch&features=Rank&q=info:$url HTTP/1.1rn";
  75. //echo “
    $out
    n”; //debug only
  76. $out .= “User-Agent: $googleuarn”;
  77. $out .= “Host: $googlehostrn”;
  78. $out .= “Connection: Closernrn”;
  79. fwrite($fp, $out);
  80. //$pagerank = substr(fgets($fp, 128), 4); //debug only
  81. //echo $pagerank; //debug only
  82. while (!feof($fp)) {
  83. $data = fgets($fp, 128);
  84. //echo $data;
  85. $pos = strpos($data, “Rank_”);
  86. if($pos === false){} else{
  87. $pr=substr($data, $pos + 9);
  88. $pr=trim($pr);
  89. $pr=str_replace(“n”,”,$pr);
  90. return $pr;
  91. }
  92. }
  93. //else { echo “$errstr ($errno) n”; } //debug only
  94. fclose($fp);
  95. }
  96. return $pr;
  97. }
  98. ?>
复制代码


陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn