返回列表 回复 发帖

php获取Google PageRank值代码

php获取google PageRank值代码,注意最新的Google PageRank API地址已变为“http://toolbarqueries.google.com ... client-auto&features=Rank:&q=info:”

  1. class PageRank {
  2. function __construct($url) {
  3. $url = 'info:' . $url;
  4. $hash = '6' . $this->c($this->e($this->b($url)));
  5. $fetch = 'http://toolbarqueries.google.com/tbr?client=navclient-auto&ch=' . $hash . '&ie=UTF-8&oe=UTF-8&features=Rank&q=' . $url;
  6. if(function_exists('curl_init')) {
  7. $ch = curl_init();
  8. curl_setopt($ch, CURLOPT_URL, $fetch);
  9. curl_setopt($ch, CURLOPT_HEADER, 0);
  10. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  11. $out = curl_exec($ch);
  12. curl_close($ch);
  13. } else {
  14. $out = file_get_contents($fetch);
  15. }
  16. $out = trim($out);
  17. if(strlen($out) > 0) {
  18. $this->pr = substr($out, 9);
  19. } else {
  20. $this->pr = -1;
  21. }
  22. }
  23. function b($hash) {
  24. $j = array();
  25. $length = strlen($hash);
  26. for($i = 0; $i < $length; $i++) {
  27. $j[$i] = ord($hash[$i]);
  28. }
  29. return $j;
  30. }
  31. function c($l) {
  32. $l = ((($l / 7) << 2) | (($this->h($l, 13)) & 7));
  33. $j = array();
  34. $j[0] = $l;
  35. for($i = 1; $i < 20; $i++) {
  36. $j[$i] = $j[$i - 1] – 9;
  37. }
  38. $l = $this->e($this->g($j), 80);
  39. return $l;
  40. }
  41. function e($hash) {
  42. $r = 3862272608;
  43. $j = count($hash);
  44. $p = 2654435769;
  45. $o = 2654435769;
  46. $n = 3862272608;
  47. $l = 0;
  48. $m = $j;
  49. $q = array();
  50. while ($m >= 12) {
  51. $p += ($hash[$l] + ( $hash[$l + 1] << 8 ) + ($hash[$l + 2] << 16) + ($hash[$l + 3] << 24));
  52. $o += ($hash[$l + 4] + ( $hash[$l + 5] << 8 ) + ($hash[$l + 6] << 16) + ($hash[$l + 7] << 24));
  53. $n += ($hash[$l + 8] + ( $hash[$l + 9] << 8 ) + ($hash[$l + 10] << 16) + ($hash[$l + 11] << 24));
  54. $q = $this->s($p, $o, $n);
  55. $p = $q[0];
  56. $o = $q[1];
  57. $n = $q[2];
  58. $l += 12;
  59. $m -= 12;
  60. }
  61. $n += $j;
  62. switch ($m) {
  63. case 11:
  64. $n += $hash[$l + 10] << 24;
  65. case 10:
  66. $n += $hash[$l + 9] << 16;
  67. case 9:
  68. $n += $hash[$l + 8] << 8;
  69. case 8:
  70. $o += $hash[$l + 7] << 24;
  71. case 7:
  72. $o += $hash[$l + 6] << 16;
  73. case 6:
  74. $o += $hash[$l + 5] << 8;
  75. case 5:
  76. $o += $hash[$l + 4];
  77. case 4:
  78. $p += $hash[$l + 3] << 24;
  79. case 3:
  80. $p += $hash[$l + 2] << 16;
  81. case 2:
  82. $p += $hash[$l + 1] << 8;
  83. case 1:
  84. $p += $hash[$l];
  85. }
  86. $q = $this->s($p, $o, $n);
  87. return ($q[2] < 0) ? (4294967296 + $q[2]) : $q[2];
  88. }
  89. function f($j, $i) {
  90. $k = 2147483648;
  91. if ($k & $j) {
  92. $j = $j >> 1;
  93. $j &= ~$k;
  94. $j |= 1073741824;
  95. $j = $j >> ($i – 1);
  96. } else {
  97. $j = $j >> $i;
  98. }
  99. return $j;
  100. }
  101. function g($j) {
  102. $l = array();
  103. $length = count($j);
  104. for($k = 0; $k < $length; $k++) {
  105. for ($m = $k * 4; $m <= $k * 4 + 3; $m++) {
  106. $l[$m] = $j[$k] & 255;
  107. $j[$k] = $this->f($j[$k], 8);
  108. }
  109. }
  110. return $l;
  111. }
  112. function h($j, $l) {
  113. $k = floor($j / $l);
  114. return ($j – $k * $l);
  115. }
  116. function s($t, $k, $u) {
  117. $t -= $k;
  118. $t -= $u;
  119. $t ^= ($this->f($u, 13));
  120. $k -= $u;
  121. $k -= $t;
  122. $k ^= ($t << 8);
  123. $u -= $t;
  124. $u -= $k;
  125. $u ^= ($this->f($k, 13));
  126. $t -= $k;
  127. $t -= $u;
  128. $t ^= ($this->f($u, 12));
  129. $k -= $u;
  130. $k -= $t;
  131. $k ^= ($t << 16);
  132. $u -= $t;
  133. $u -= $k;
  134. $u ^= ($this->f($k, 5));
  135. $t -= $k;
  136. $t -= $u;
  137. $t ^= ($this->f($u, 3));
  138. $k -= $u;
  139. $k -= $t;
  140. $k ^= ($t << 10);
  141. $u -= $t;
  142. $u -= $k;
  143. $u ^= ($this->f($k, 15));
  144. return array($t, $k, $u);
  145. }
  146. }
  147. $pr = new PageRank('http://www.xinran001.com');
  148. echo $pr->pr;
复制代码
最核心的地方就是这个checknum算法,有了这段代码你也可以建一个PR值查询工具。

本站PR值查询地址:http://www.hujuntao.com/api/pr/
返回列表