Google PR value of the PHP implementation code

Source: Internet
Author: User
  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. Converts a string to a 32-bit integer
  7. function Strtonum ($Str, $Check, $Magic) {
  8. $Int 32Unit = 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 integers (usually +/-2.15e+9 = 2^31),
  13. The result of converting to an integer is undefined
  14. Refer to Http://www.php.net/manual/en/language.types.integer.php
  15. if ($Check >= $Int 32Unit) {
  16. $Check = ($Check-$Int 32Unit * (int) ($Check/$Int 32Unit));
  17. If the check less than-2^31
  18. $Check = ($Check <-2147483648)? ($Check + $Int 32Unit): $Check;
  19. }
  20. $Check + = Ord ($Str {$i});
  21. }
  22. return $Check;
  23. }
  24. Hash-encode a URL
  25. function Hashurl ($String) {
  26. $Check 1 = strtonum ($String, 0x1505, 0x21);
  27. $Check 2 = strtonum ($String, 0, 0x1003f);
  28. $Check 1 >>= 2;
  29. $Check 1 = (($Check 1 >> 4) & 0x3ffffc0) | ($Check 1 & 0x3F);
  30. $Check 1 = (($Check 1 >> 4) & 0x3ffc00) | ($Check 1 & 0x3ff);
  31. $Check 1 = (($Check 1 >> 4) & 0x3c000) | ($Check 1 & 0x3fff);
  32. $T 1 = ((($Check 1 & 0x3c0) << 4) | ($Check 1 & 0x3C)) <<2) | ($Check 2 & 0xf0f);
  33. $T 2 = ((($Check 1 & 0xffffc000) << 4) | ($Check 1 & 0x3c00)) << 0xA) | ($Check 2 & 0xf0f0000);
  34. Return ($T 1 | $T 2);
  35. }
  36. Generate a checksum for a hash string
  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. Returns the PageRank hash check code
  64. function Getch ($url) {return CheckHash (Hashurl ($url));}
  65. Return PR Value
  66. function Getpr ($url) {
  67. Global $googlehost, $googleua;
  68. $ch = Getch ($url);
  69. $fp = Fsockopen ($googlehost, $errno, $errstr, 30);
  70. if ($fp) {
  71. $out = "Get/search?client=navclient-auto&ch= $ch &features=rank&q=info: $url http/1.1\r\n";
  72. echo "
    $out
    \ n "; Debug only
  73. $out. = "User-agent: $googleua \ r \ n";
  74. $out. = "Host: $googlehost \ r \ n";
  75. $out. = "connection:close\r\n\r\n";
  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. Generate PageRank Graphics
  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 = ";
  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. ?>
Copy Code
  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.