首頁 >後端開發 >php教程 >Google PR值的PHP實作程式碼

Google PR值的PHP實作程式碼

WBOY
WBOY原創
2016-07-25 08:57:491205瀏覽
  1. //HM2K 的PageRank Lookup v1.1(更新:31/01/07)
  2. //基於發現的演算法此處:http://pagerank.gamesaga.net/
  3. //設定- 主機與使用者代理程式
  4. $googlehost='toolbarqueries.google.com';
  5. $ua='Mozilla/5.0 (google Windows; U; Windows NT 5.1; en-US; rv:1.8.0.6) Gecko/20060728 Firefox/1.5';
  6. //字串轉換為32位元整數
  7. function StrToNum($Str ,ToNum($Str ,ToNum($Str ,ToNum($Str ,ToNum($Str ,ToNum($Str ,ToNum($Str ,ToNum($Str ,ToNum($Str),ToNum($Str ,ToNum($Str ,ToNum($Str) $Check, $Magic) {
  8. $Int32Unit = 4294967296; // 2^32
  9. $length = strlen($Str);
  10. for ($i = 0; $i $Check *= $Magic ;
  11. //如果浮點數超出整數範圍(通常為+/- 2.15e+9 = 2^31),
  12. // 轉換為整數的結果是未定義的
  13. // 參考http ://www.php.net/manual/en/language.types.integer.php
  14. if ($Check >= $Int32Unit) {
  15. $檢查 = ($Check - $Int32Unit * (int) ($Check / $Int32Unit));
  16. //如果支票小於-2^31
  17. $Check = ($Check }
  18. $Check += ord($Str{$i});
  19. }
  20. return $Check;
  21. }
  22. //將URL進行空間編碼
  23. function HashURL($String) {
  24. $Check1 = StrToNum($String, 0x1505, 0x21);
  25. $Check2 = StrToNum($String, 0, 0x1003F);
  26. $Check1 >>= 2;
  27. $Check1 = (($Check1 >> 4) & 0x3FFFFC0 ) | ($Check1 & 0x3F);
  28. $Check1 = (($Check1 >> 4 ) & 0x3FFC00 ) | ($Check1 & 0x3FF);
  29. $Check1 = (($Check1 >> 4) & 0x3C000 ) | ($Check1 & 0x3FFF);
  30. $T1 = ((((((() $Check1 & 0x3C0) $T2 = (((($Check1 & 0xFFFFC000)
  31. return ($T1 | $T2);
  32. }
  33. //為雜湊字串產生校驗碼
  34. function CheckHash($雜湊值) {
  35. $CheckByte = 0;
  36. $Flag = 0;
  37. $HashStr = sprintf('%u', $Hashnum ) ;
  38. $length = strlen($HashStr) ;
  39. for ($i = $length - 1; $i >= 0; $i --) {
  40. $Re = $HashStr {$i};
  41. if (1 = == ($Flag % 2)) {
  42. $Re += $Re;
  43. $Re = (int)($Re / 10) + ($ Re % 10);
  44. }
  45. $CheckByte += $Re;
  46. $Flag ++;
  47. }
  48. $CheckByte %= 10;
  49. if (0 ! == $CheckByte) {
  50. $CheckByte = 10 - $CheckByte;
  51. if (1 === ( $Flag % 2) ) {
  52. if (1 === ($CheckByte % 2)) {
  53. $CheckByte += 9;
  54. }
  55. $CheckByte >>= 1;
  56. }
  57. }
  58. return '7'.$CheckByte.$HashStr;
  59. }
  60. //返回pagerank哈希校驗碼
  61. function getch($ url) { return CheckHash(HashURL($url)); }
  62. //返回PR值
  63. function getpr($url) {
  64. global $googlehost,$googleua;
  65. $ch = getch($url);
  66. $fp = fsockopen($googlehost, 80, $errno, $errstr , 30);
  67. if ($fp) {
  68. $out = "GET /search?client=navclient-auto&ch=$ch&features=Rank&q=info:$ url HTTP/1.1rn";
  69. // echo "
    $out
    n"; //僅調試
  70. $out .= "用戶代理: $googleuarn";
  71. $out .= "主機: $googlehostrn";
  72. $out .= "連接: Closernrn";
  73. fwrite($fp, $out);
  74. //$pagerank = substr(fgets($fp, 128), 4); / /僅調試
  75. //echo $pagerank; //僅調試
  76. while (!feof($fp)) {
  77. $data = fgets($fp, 128);
  78. //echo $ data;
  79. $pos = strpos($data , "Rank_");
  80. if($pos === false){} else{
  81. $pr=substr($data, $pos + 9) ;
  82. $pr=trim($pr) ;
  83. $pr=str_replace("n",'',$pr);
  84. return $pr;
  85. }
  86. }
  87. //else { echo "$errstr ($errno)
    n"; } //僅調試
  88. fclose($fp);
  89. }
  90. }
  91. //生成pagerank圖形
  92. function pagerank($url,$width=40,$method= '風格') {
  93. if (!preg_match('/^(http://)?([^/]+)/ i', $url)) { $url='http://'.$網址; }
  94. $pr=getpr($url);
  95. $pagerank="PageRank: $pr/10";
  96. //(舊)圖像方法
  97. if ($method = = '圖片') {
  98. $prpos=$width*$pr/10;
  99. $prneg=$width-$ prpos;
  100. $html=''.$ pagerank.'Google PR值的PHP實作程式碼';
  101. }
  102. //預樣式方法
  103. if ($method == 'style') {
  104. $prpercent=100*$pr/10;
  105. $html='
    ';
  106. }
  107. $out=' '.$html.' ';
  108. return $out;
  109. }
  110. if ((!isset($_POST['url'])) && (!isset($ _GET['url']))) { echo ''; }
  111. if (isset($_REQUEST['url'])) { echo pagerank($_REQUEST['url']); }
  112. ?>
複製程式碼


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