查看: 18687|回复: 1

用 PHP 获取 Alexa Rank 、 Google PageRank 、 Sogou Rank 和 ChinaRank ……

[复制链接]
发表于 2007-10-5 16:27:21 | 显示全部楼层 |阅读模式
台州网址导航
给出使用方法……

Usage:

注意:
域名不要带协议,
比如 http://www.quchao.com 请去掉 ‘http://’

当前仅支持 alexa 、 google 、 sogou 、 chinarank 四个类型名
其它值将被忽略

一下是调用的例子……

class_rank.php:
  1.   
  2. /**
  3. * Rank class
  4. */
  5. class Rank
  6. {
  7. //    {{{    properties
  8.   
  9. /**
  10. * Domain to process.
  11. * @var        string
  12. */
  13. var $_domain = 'www.quchao.com';
  14.   
  15. /**
  16. * Error Message.
  17. * @var        string
  18. */
  19. var $_error = null;
  20.   
  21. //    {{{    constructor
  22.   
  23. /**
  24. * Constructor.
  25. * @param    string    Domain to process
  26. * @return    void
  27. */
  28. function Rank($domain = 'www.quchao.com')
  29. {
  30. if(preg_match('/^(?:[^\W_](?:[^\W_]|-){0,61}[^\W_]\.)+[a-zA-Z]{2,6}\.?$/', $domain)) {
  31. $this->_domain = $domain;
  32. } else {
  33. $this->_error = 'Invalid Domain!';
  34. }
  35. }
  36.   
  37. //    }}}
  38.   
  39. //    {{{    destructor
  40.   
  41. /**
  42. * Destructor.
  43. * @return    void
  44. */
  45. function __destruct()
  46. {
  47. }
  48.   
  49. //    }}}
  50.   
  51. //    {{{    process()
  52.   
  53. /**
  54. * Fetch the alexa rank.
  55. * @param    string    Type of rank (alexa, google, sogou)
  56. * @return    mixed    Rank value
  57. */
  58. function process($type = 'alexa')
  59. {
  60. if(in_array(strtolower($type), array('alexa', 'google', 'sogou', 'chinarank'))) {
  61. return $this->$type();
  62. } else {
  63. $this->_error = 'Non-supported Rank Type!';
  64. return 0;
  65. }
  66. }
  67.   
  68. //    }}}
  69.   
  70. //    {{{    alexa()
  71.   
  72. /**
  73. * Fetch the alexa rank.
  74. * @return    mixed    Rank value
  75. */
  76. function alexa()
  77. {
  78. $result = $this->fetch('http://data.alexa.com/data/+wQ411en8000lA?cli=10&dat=snba&ver=7.0&cdt=alx_vw%3D20%26wid%3D12206%26act%3D00000000000%26ss%3D1680x16t%3D0%26ttl%3D35371%26vis%3D1%26rq%3D4&url=' . urlencode($this->_domain));
  79. if(preg_match('/TEXT=\"(\d+)\"\/>/', $result, $match)) {
  80. return $match[1];
  81. } else {
  82. $this->_error = 'Failed to fetch alexa rank!';
  83. return 0;
  84. }
  85. }
  86.   
  87. //    }}}
  88.   
  89. //    {{{    google()
  90.   
  91. /**
  92. * Fetch the google rank.
  93. * @return    mixed    Rank value
  94. */
  95. function google()
  96. {
  97. $result = $this->fetch('http://www.google.com/search?client=navclient-auto&ch=6' . $this->GCH($this->strord('info:' . $this->_domain)) . '&ie=UTF-8&oe=UTF-8&features=Rank&q=info:' . urlencode($this->_domain));
  98. if (preg_match('/\d+:\d+:(\d+)/', $result, $match)) {
  99. return $match[1];
  100. } else {
  101. $this->_error = 'Failed to fetch google rank!';
  102. return 0;
  103. }
  104. }
  105.   
  106. //    }}}
  107.   
  108. //    {{{    sogou()
  109.   
  110. /**
  111. * Fetch the sogou rank.
  112. * @return    mixed    Rank value
  113. */
  114. function sogou()
  115. {
  116. $result = $this->fetch('http://www.sogou.com/web?query=link%3A' . urlencode($this->_domain));
  117. if(preg_match('/<\/span>(\d+)<\/dd>/', $result, $match)) {
  118. return $match[1];
  119. } else {
  120. $this->_error = 'Failed to fetch sogou rank!';
  121. return 0;
  122. }
  123. }
  124.   
  125. //    }}}
  126.   
  127. //    {{{    chinarank()
  128.   
  129. /**
  130. * Fetch the ChinaRank rank.
  131. * @return    mixed    Rank value
  132. */
  133. function chinarank()
  134. {
  135. list($msec, $sec) = split(' ', microtime());
  136. $r = $sec . substr($msec, 2, 3);
  137. $result = $this->fetch('http://www.chinarank.org.cn/overview/Info.do?url=' . urlencode($this->_domain) . '&r=' . $r);
  138. if(preg_match('/class=\"domain\">(\d+)<\/span>/', $result, $match)) {
  139. return $match[1];
  140. } else {
  141. $this->_error = 'Failed to fetch chinarank!';
  142. return 0;
  143. }
  144. }
  145.   
  146. //    }}}
  147.   
  148. //    {{{    fetch()
  149.   
  150. /**
  151. * Fetch the remote content.
  152. * @param    string    URL of the page
  153. * @return    string    Content of the page
  154. */
  155. function fetch($url)
  156. {
  157. $content = '';
  158. if(function_exists('curl_init')) {
  159. //Curl Method
  160. $handle = curl_init();
  161. if(!$handle) {
  162. return false;
  163. }
  164. curl_setopt($handle, CURLOPT_URL, $url);
  165. curl_setopt($handle, CURLOPT_CONNECTTIMEOUT, 5);
  166. curl_setopt($handle, CURLOPT_RETURNTRANSFER, 1);
  167. curl_setopt($handle, CURLOPT_FOLLOWLOCATION, 0);
  168. $content = curl_exec($handle);
  169. curl_close($handle);
  170. } elseif(ini_get('allow_url_fopen')) {
  171. //File Method
  172. $handle = @fopen($url, 'r');
  173. if(!$handle) {
  174. return false;
  175. }
  176. while($buffer = fgets($handle, 4096)) {
  177. $content .= $buffer;
  178. }
  179. fclose($handle);
  180. } elseif(function_exists('fsockopen')) {
  181. //Socket Method
  182. $pos = strpos($url, '://');
  183. $host = substr($url, $pos + 3, strpos($url, '/', $pos + 3) - $pos - 3);
  184. $uri = substr($url, strpos($url, '/', $pos + 3));
  185. $request = "GET " . $uri . " HTTP/1.1\r\n"
  186. . "Host: " . $host . "\r\n"
  187. . "Accept: */*\r\n"
  188. . "User-Agent: Mozilla/4.0 (compatible; MSIE 5.5; Windows 98)\r\n"
  189. . "\r\n";
  190. $handle = @fsockopen($host, 80, $errno, $errstr, 30);
  191. if(!$handle) {
  192. return false;
  193. }
  194. @fputs($handle, $request);
  195. while(!feof($handle)) {
  196. $content .= fgets($handle, 4096);
  197. }
  198. fclose($handle);
  199. $separator = strpos($content, "\r\n\r\n");
  200. if($separator === false) {
  201. return $content;
  202. } else {
  203. return substr($content, $separator + 4);
  204. }
  205. }
  206. return $content;
  207. }
  208.   
  209. //    }}}
  210.   
  211. //    {{{    strord()
  212.   
  213. function strord($string)
  214. {
  215. $strlen = strlen($string);
  216. for($i = 0; $i < $strlen; $i++) {
  217. $result[$i] = ord($string{$i});
  218. }
  219. return $result;
  220. }
  221.   
  222. //    }}}
  223.   
  224. //    {{{    GCH()
  225.   
  226. function GCH($url, $length=null)
  227. {
  228. $length = sizeof($url);
  229. $a = $b = 0x9E3779B9;
  230. $c = 0xE6359A60;
  231. $k = 0;
  232. $len = $length;
  233. while($len >= 12) {
  234. $a += ($url[$k + 0] + ($url[$k + 1] << 8) + ($url[$k + 2] << 16) + ($url[$k + 3] << 24));
  235. $b += ($url[$k + 4] + ($url[$k + 5] << 8) + ($url[$k + 6] << 16) + ($url[$k + 7] << 24));
  236. $c += ($url[$k + 8] + ($url[$k + 9] << 8) + ($url[$k + 10] << 16) + ($url[$k + 11] << 24));
  237. $mix = $this->mix($a, $b, $c);
  238. $a = $mix[0];
  239. $b = $mix[1];
  240. $c = $mix[2];
  241. $k += 12;
  242. $len -= 12;
  243. }
  244. $c += $length;
  245. //All the case statements fall through
  246. switch($len) {
  247. case 11: $c += ($url[$k + 10] << 24);
  248. case 10: $c += ($url[$k + 9] << 16);
  249. case 9 : $c += ($url[$k + 8] << 8);
  250. //The first byte of c is reserved for the length
  251. case 8 : $b += ($url[$k + 7] << 24);
  252. case 7 : $b += ($url[$k + 6] << 16);
  253. case 6 : $b += ($url[$k + 5] << 8);
  254. case 5 : $b += ($url[$k + 4]);
  255. case 4 : $a += ($url[$k + 3] << 24);
  256. case 3 : $a += ($url[$k + 2] << 16);
  257. case 2 : $a += ($url[$k + 1] << 8);
  258. case 1 : $a += ($url[$k + 0]);
  259. //Case 0: nothing left to add
  260. }
  261. $mix = $this->mix($a, $b, $c);
  262. //Report the result
  263. return $mix[2];
  264. }
  265.   
  266. //    }}}
  267.   
  268. //    {{{    mix()
  269.   
  270. function mix($a, $b, $c){
  271. $a -= $b;
  272. $a -= $c;
  273. $a ^= ($this->zeroFill($c, 13));
  274. $b -= $c;
  275. $b -= $a;
  276. $b ^= ($a << 8);
  277. $c -= $a;
  278. $c -= $b;
  279. $c ^= ($this->zeroFill($b, 13));
  280. $a -= $b;
  281. $a -= $c;
  282. $a ^= ($this->zeroFill($c, 12));
  283. $b -= $c;
  284. $b -= $a;
  285. $b ^= ($a << 16);
  286. $c -= $a;
  287. $c -= $b;
  288. $c ^= ($this->zeroFill($b, 5));
  289. $a -= $b;
  290. $a -= $c;
  291. $a ^= ($this->zeroFill($c, 3));
  292. $b -= $c;
  293. $b -= $a;
  294. $b ^= ($a << 10);
  295. $c -= $a;
  296. $c -= $b;
  297. $c ^= ($this->zeroFill($b, 15));
  298. return array($a, $b, $c);
  299. }
  300.   
  301. //    }}}
  302.   
  303. //    {{{    zeroFill()
  304.   
  305. function zeroFill($a, $b)
  306. {
  307. $z = hexdec(80000000);
  308. if($z & $a) {
  309. $a = ($a >> 1);
  310. $a &= (~ $z);
  311. $a |= 0x40000000;
  312. $a = ($a >> ($b - 1));
  313. } else{
  314. $a = ($a>>$b);
  315. }
  316. return $a;
  317. }
  318.   
  319. //    }}}
  320.   
  321. }
  322.   
  323. ?>
复制代码
  1. < ?php

  2. include './class_rank.php';

  3. // set up your domain
  4. $rank = new Rank('www.quchao.com');

  5. // output the alexa rank
  6. echo $rank->process('alexa');

  7. // output the google page rank
  8. echo $rank->process('google');

  9. // output the sogou page rank
  10. echo $rank->process('sogou');

  11. // output the chinarank
  12. echo $rank->process('chinarank');
  13. ?>
复制代码
台州维博网络(www.tzweb.com)专门运用PHP+MYSQL/ASP.NET+MSSQL技术开发网站门户平台系统等。
 楼主| 发表于 2007-10-5 16:27:38 | 显示全部楼层
台州网址导航
不记得以前在哪个论坛见过这种 XHTML + CSS 的 Page Rank Bar ……
(好像是经典?可以通过 XHTML 的验证……)
这次在 CoolCode 那边再次看到使用……
于是存了下来……
现在贴出本部落格使用的样式表……
与传统不同的部分只是添加了一象素的 Padding ……
个人觉得这样处理会好看很多……
不说了……
给出代码……

getRank.css:
.rank{margin-left:15px;margin-right:15px;margin-bottom:15px;}
.rank_container {width: 80px;text-align: center;font-size: 12px;}
.rank_border {border: 1px solid #999999;background-color: #FFFFFF;padding: 1px;margin: 0;text-align: left;display: block;}
.rank_bar {width: 80px;height: 6px;border: 0;padding: 0;margin: 0;font-size: 0;display: block;}
.rank_rate {width: 0px;height: 6px;padding: 0;margin: 0;border: 0;background-color: #F58EBD;display: block;}
#alexa_rank {background-color: #F58EBD;}
#google_rank {background-color: #FFBB3C;}
#sogou_rank {background-color: #A2BE1B;}
#chinarank_rank {background-color: #5392CE;}
.clearer{clear:both;}


还是超文本部分:
getRank.html:
<p class="rank">                 <span class="rank_container" id="alexa_container">Alexa Rank</span>
<span class="rank_bar">
<span class="rank_border">
<span class="rank_rate" id="alexa_rank"></span>
</span>
</span>
  
<p class="rank">                 <span class="rank_container" id="google_container">Google Rank</span>
<span class="rank_bar">
<span class="rank_border">
<span class="rank_rate" id="google_rank"></span>
</span>
</span>
  
<p class="rank">                 <span class="rank_container" id="sogou_container">Sogou Rank</span>
<span class="rank_bar">
<span class="rank_border">
<span class="rank_rate" id="sogou_rank"></span>
</span>
</span>
  
<p class="rank">                 <span class="rank_container" id="chinarank_container">ChinaRank</span>
<span class="rank_bar">
<span class="rank_border">
<span class="rank_rate" id="chinarank_rank"></span>
</span>
</span>

其它的……
大伙儿自己去玩吧……

我直接用文本方式来储存两个排名值……
然后用 filemtime() 函式来判断更新时间……
最简单的缓存而已嘛……
UNIX 服务器用户甚至可以用 Cron 直接缓存排名值到 JS 文件……
等于每次调用的就只是调用静态文件而已……
而省去了对于更新时间的判断……
岂不更??

我的代码:

cache.php:
<?php
  
if(! function_exists("file_put_contents")) {
function file_put_contents($filename, $text) {
$fp = fopen($filename,"w");
fwrite($fp, $text);
fclose($fp);
}
}
  
// updated every 24 hours, or use 'force_update = true' to update it manually
if (@filemtime('rank.cache') &lt; time() - 86400 || $_GET['force_update']) {
// write rank cache
include './class_rank.php';
$rank = new Rank('www.quchao.com');
$ranks = array(
$rank-&gt;process('alexa'),
$rank-&gt;process('google'),
$rank-&gt;process('sogou'),
$rank-&gt;process('chinarank'),
);
@file_put_contents('rank.cache', implode("\n", $ranks));
} else {
// get rank cache
$ranks = @file('rank.cache');
}
?>

[ 本帖最后由 tznktg 于 2007-10-5 16:28 编辑 ]
台州维博网络(www.tzweb.com)专门运用PHP+MYSQL/ASP.NET+MSSQL技术开发网站门户平台系统等。
您需要登录后才可以回帖 登录 | 注册

本版积分规则

网站推广
关于我们
  • 台州维博网络(Tzweb.com)拥有多年开发网站平台系统门户手机客户端等业务的成功经验。主要从事:企业网站建设、网站程序开发、手机APP客户端、平面设计、主机域名、虚拟空间、网站推广、网站优化、后期维护等服务,满足不同企业公司的需求,是台州地区领先的网络技术服务商!

Hi,扫描关注我

Copyright © 2005-2024 站长论坛 All rights reserved

Powered by 站长论坛 with TZWEB Update Techonolgy Support

快速回复 返回顶部 返回列表