PHP implementation code of GooglePR value

Source: Internet
Author: User
PHP implementation code of GooglePR value

  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/123456 ';
  6. // Convert the string to a 32-bit integer
  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. // Hash the 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. // Generate a verification code for the 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 verification code.
  64. Function getch ($ url) {return CheckHash (HashURL ($ url ));}
  65. // Returns the prvalue.
  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.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 ";
  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. ?>

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.