查看: 7193|回复: 1
打印 上一主题 下一主题

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

[复制链接]
跳转到指定楼层
1#
发表于 2007-10-9 11:17:19 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
台州网址导航
给出使用方法……

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. ?>
复制代码
分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏 转播转播 分享分享 分享淘帖
台州维博网络(www.tzweb.com)专门运用PHP+MYSQL/ASP.NET+MSSQL技术开发网站门户平台系统等。
2#
 楼主| 发表于 2007-10-9 11:53:18 | 只看该作者
台州网址导航
不记得以前在哪个论坛见过这种 XHTML + CSS 的 Page Rank Bar ……
(好像是经典?可以通过 XHTML 的验证……)
这次在 CoolCode 那边再次看到使用……
于是存了下来……
现在贴出本部落格使用的样式表……
与传统不同的部分只是添加了一象素的 Padding ……
个人觉得这样处理会好看很多……
不说了……
给出代码……

getRank.css:
  1. .rank{margin-left:15px;margin-right:15px;margin-bottom:15px;}
  2. .rank_container {width: 80px;text-align: center;font-size: 12px;}
  3. .rank_border {border: 1px solid #999999;background-color: #FFFFFF;padding: 1px;margin: 0;text-align: left;display: block;}
  4. .rank_bar {width: 80px;height: 6px;border: 0;padding: 0;margin: 0;font-size: 0;display: block;}
  5. .rank_rate {width: 0px;height: 6px;padding: 0;margin: 0;border: 0;background-color: #F58EBD;display: block;}
  6. #alexa_rank {background-color: #F58EBD;}
  7. #google_rank {background-color: #FFBB3C;}
  8. #sogou_rank {background-color: #A2BE1B;}
  9. #chinarank_rank {background-color: #5392CE;}
  10. .clearer{clear:both;}
复制代码
还是超文本部分:
getRank.html:
  1.                  Alexa Rank





  2.   
  3.                  Google Rank





  4.   
  5.                  Sogou Rank





  6.   
  7.                  ChinaRank




复制代码
其它的……
大伙儿自己去玩吧……

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

我的代码:

cache.php:
  1. < ?php
  2.   
  3. if(! function_exists("file_put_contents")) {
  4. function file_put_contents($filename, $text) {
  5. $fp = fopen($filename,"w");
  6. fwrite($fp, $text);
  7. fclose($fp);
  8. }
  9. }
  10.   
  11. // updated every 24 hours, or use 'force_update = true' to update it manually
  12. if (@filemtime('rank.cache') < time() - 86400 || $_GET['force_update']) {
  13. // write rank cache
  14. include './class_rank.php';
  15. $rank = new Rank('www.quchao.com');
  16. $ranks = array(
  17. $rank->process('alexa'),
  18. $rank->process('google'),
  19. $rank->process('sogou'),
  20. $rank->process('chinarank'),
  21. );
  22. @file_put_contents('rank.cache', implode("\n", $ranks));
  23. } else {
  24. // get rank cache
  25. $ranks = @file('rank.cache');
  26. }
  27. ?>
复制代码
台州维博网络(www.tzweb.com)专门运用PHP+MYSQL/ASP.NET+MSSQL技术开发网站门户平台系统等。
您需要登录后才可以回帖 登录 | 注册

本版积分规则

网站推广
关于我们
  • 台州朗动科技(Tzweb.com)拥有多年开发网站平台系统门户手机客户端等业务的成功经验。主要从事:政企网站,系统平台,微信公众号,各类小程序,手机APP客户端,浙里办微应用,浙政钉微应用、主机域名、虚拟空间、后期维护等服务,满足不同企业公司的需求,是台州地区领先的网络技术服务商!

Hi,扫描关注我

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

Powered by 站长论坛 with TZWEB Update Techonolgy Support

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