PHPでGoogleページランキングを調べる
公開日| 2009年11月13日 | コメントはまだありません。
カテゴリー:php |
概要 : 今回の記事は、Google ツールバーで表示できるGoogleのペーランクをPHPを用いて取得する方法です。
これと同じような記事は、多々見受けられますが、うまく動作しない場合も、多いようです。 ここでは、可能な範囲で異なるOSで確認した結果、Linux,FreeBSD,Windowsで動作してものを、簡単にご紹介したいと思います。
では、早速、サンプルソースを使って試してみましょう。
元記事:
使ってみよう
上記のダウンロード先から、ダウンロードすると、pagerank.phpsというファイルがダウンロードできます。
このファイル名の拡張子を".phps"から".php"へ変更し、適当なWEBサイトへアップロードします。※拡張子が".phps"でも動作するように設定されていれば、そのままでOKです。
簡単に動作確認するために、以下のようなサンプルプログラムを作成してみました。
では、早速、ソースコードを見てみましょう。
[pagerank.php]
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 | //PageRank Lookup v1.1 by HM2K (update: 31/01/07) //based on an alogoritham found here: http://pagerank.gamesaga.net/ //settings - host and user agent $googlehost='toolbarqueries.google.com'; $googleua='Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.0.6) Gecko/20060728 Firefox/1.5'; //convert a string to a 32-bit integer function StrToNum($Str, $Check, $Magic) { $Int32Unit = 4294967296; // 2^32 $length = strlen($Str); for ($i = 0; $i < $length; $i++) { $Check *= $Magic; //If the float is beyond the boundaries of integer (usually +/- 2.15e+9 = 2^31), // the result of converting to integer is undefined // refer to http://www.php.net/manual/en/language.types.integer.php if ($Check >= $Int32Unit) { $Check = ($Check - $Int32Unit * (int) ($Check / $Int32Unit)); //if the check less than -2^31 $Check = ($Check < -2147483648) ? ($Check + $Int32Unit) : $Check; } $Check += ord($Str{$i}); } return $Check; } //genearate a hash for a url function HashURL($String) { $Check1 = StrToNum($String, 0x1505, 0x21); $Check2 = StrToNum($String, 0, 0x1003F); $Check1 >>= 2; $Check1 = (($Check1 >> 4) & 0x3FFFFC0 ) | ($Check1 & 0x3F); $Check1 = (($Check1 >> 4) & 0x3FFC00 ) | ($Check1 & 0x3FF); $Check1 = (($Check1 >> 4) & 0x3C000 ) | ($Check1 & 0x3FFF); $T1 = (((($Check1 & 0x3C0) << 4) | ($Check1 & 0x3C)) <<2 ) | ($Check2 & 0xF0F ); $T2 = (((($Check1 & 0xFFFFC000) << 4) | ($Check1 & 0x3C00)) << 0xA) | ($Check2 & 0xF0F0000 ); return ($T1 | $T2); } //genearate a checksum for the hash string function CheckHash($Hashnum) { $CheckByte = 0; $Flag = 0; $HashStr = sprintf('%u', $Hashnum) ; $length = strlen($HashStr); for ($i = $length - 1; $i >= 0; $i --) { $Re = $HashStr{$i}; if (1 === ($Flag % 2)) { $Re += $Re; $Re = (int)($Re / 10) + ($Re % 10); } $CheckByte += $Re; $Flag ++; } $CheckByte %= 10; if (0 !== $CheckByte) { $CheckByte = 10 - $CheckByte; if (1 === ($Flag % 2) ) { if (1 === ($CheckByte % 2)) { $CheckByte += 9; } $CheckByte >>= 1; } } return '7'.$CheckByte.$HashStr; } //return the pagerank checksum hash function getch($url) { return CheckHash(HashURL($url)); } //return the pagerank figure function getpr($url) { global $googlehost,$googleua; $ch = getch($url); $fp = fsockopen($googlehost, 80, $errno, $errstr, 30); if ($fp) { $out = "GET /search?client=navclient-auto&ch=$ch&features=Rank&q=info:$url HTTP/1.1\r\n"; //echo "<pre>$out</pre>\n"; //debug only $out .= "User-Agent: $googleua\r\n"; $out .= "Host: $googlehost\r\n"; $out .= "Connection: Close\r\n\r\n"; fwrite($fp, $out); //$pagerank = substr(fgets($fp, 128), 4); //debug only //echo $pagerank; //debug only while (!feof($fp)) { $data = fgets($fp, 128); //echo $data; $pos = strpos($data, "Rank_"); if($pos === false){} else{ $pr=substr($data, $pos + 9); $pr=trim($pr); $pr=str_replace("\n",'',$pr); return $pr; } } //else { echo "$errstr ($errno)<br />\n"; } //debug only fclose($fp); } } //generate the graphical pagerank function pagerank($url,$width=40,$method='style') { if (!preg_match('/^(http:\/\/)?([^\/]+)/i', $url)) { $url='http://'.$url; } $pr=getpr($url); $pagerank="PageRank: $pr/10"; //The (old) image method if ($method == 'image') { $prpos=$width*$pr/10; $prneg=$width-$prpos; $html='<img src="http://www.google.com/images/pos.gif" width='.$prpos.' height=4 border=0 alt="'.$pagerank.'"><img src="http://www.google.com/images/neg.gif" width='.$prneg.' height=4 border=0 alt="'.$pagerank.'">'; } //The pre-styled method if ($method == 'style') { $prpercent=100*$pr/10; $html='<div style="position: relative; width: '.$width.'px; padding: 0; background: #D9D9D9;"><strong style="width: '.$prpercent.'%; display: block; position: relative; background: #5EAA5E; text-align: center; color: #333; height: 4px; line-height: 4px;"><span></span></strong></div>'; } $out='<a href="'.$url.'" title="'.$pagerank.'">'.$html.'</a>'; return $out; } //if ((!isset($_POST['url'])) && (!isset($_GET['url']))) { echo '<form action="" method="post"><input name="url" type="text"><input type="submit" name="Submit" value="Get Pagerank"></form>'; } if (isset($_REQUEST['url'])) { echo pagerank($_REQUEST['url']); } |
このプログラムは、以下のURLを発行して結果を得ているだけです。
以下の例は、"http://example.com"のページランクを調べる時の例です。
http://toolbarqueries.google.com/search?client=navclient-auto&ch=762193099611&features=Rank&q=info:http://example.com
一番、この処理でやっかいなのが、chというパラメータの値を算出するために、ほとんど費やされています。
興味があれば、深くみられるのも良いと思います。
※この処理は、非公式な論理のため、必ずしも正しいとは限りません。また、この処理は、いつ使えなくなるかもわかりません。
先のURLを直接WEBブラウザに入力することでも、ページランクを取得することができます。
では、URLを入力してみましょう。

以下のような結果が表示されました。

3番目の数値が、ページランクになります。ここでは、7がページランクということになります。
先にも記述したようにchの値が、最も重要で、もし適当な値を指定すると、以下のように403が返信されます。

では、上記のphpのプログラムを利用してみましょう。
http://off-soft.toypark.in/?lang=ja&url=http://example.com
以下のような画像が表示されます。(少し見せ方を変更しています)

今回の記事では、PHPについて、少し知識が必要です。詳しく知りたい方は、以下の本なども良いと思います。本から学ぶことは多いと思います。ネットだけでは判らない様々な事に気づかされます。
![]() PHPによるWebアプリケーションスーパーサンプル 第2版 | ![]() Webサイト制作者のための PHP入門講座 | ![]() よくわかるPHPの教科書 | ![]() ゼロからできる PHP+MySQL Webシステム構築 | ![]() Webアプリケーション構築入門(第2版) - 実践!Webページ制作からマッシュアップまで |






