|

给出使用方法……
Usage:
注意:
域名不要带协议,
比如 http://www.quchao.com 请去掉 ‘http://’
当前仅支持 alexa 、 google 、 sogou 、 chinarank 四个类型名
其它值将被忽略
一下是调用的例子……
class_rank.php:-
- /**
- * Rank class
- */
- class Rank
- {
- // {{{ properties
-
- /**
- * Domain to process.
- * @var string
- */
- var $_domain = 'www.quchao.com';
-
- /**
- * Error Message.
- * @var string
- */
- var $_error = null;
-
- // {{{ constructor
-
- /**
- * Constructor.
- * @param string Domain to process
- * @return void
- */
- function Rank($domain = 'www.quchao.com')
- {
- if(preg_match('/^(?:[^\W_](?:[^\W_]|-){0,61}[^\W_]\.)+[a-zA-Z]{2,6}\.?$/', $domain)) {
- $this->_domain = $domain;
- } else {
- $this->_error = 'Invalid Domain!';
- }
- }
-
- // }}}
-
- // {{{ destructor
-
- /**
- * Destructor.
- * @return void
- */
- function __destruct()
- {
- }
-
- // }}}
-
- // {{{ process()
-
- /**
- * Fetch the alexa rank.
- * @param string Type of rank (alexa, google, sogou)
- * @return mixed Rank value
- */
- function process($type = 'alexa')
- {
- if(in_array(strtolower($type), array('alexa', 'google', 'sogou', 'chinarank'))) {
- return $this->$type();
- } else {
- $this->_error = 'Non-supported Rank Type!';
- return 0;
- }
- }
-
- // }}}
-
- // {{{ alexa()
-
- /**
- * Fetch the alexa rank.
- * @return mixed Rank value
- */
- function alexa()
- {
- $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));
- if(preg_match('/TEXT=\"(\d+)\"\/>/', $result, $match)) {
- return $match[1];
- } else {
- $this->_error = 'Failed to fetch alexa rank!';
- return 0;
- }
- }
-
- // }}}
-
- // {{{ google()
-
- /**
- * Fetch the google rank.
- * @return mixed Rank value
- */
- function google()
- {
- $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));
- if (preg_match('/\d+:\d+:(\d+)/', $result, $match)) {
- return $match[1];
- } else {
- $this->_error = 'Failed to fetch google rank!';
- return 0;
- }
- }
-
- // }}}
-
- // {{{ sogou()
-
- /**
- * Fetch the sogou rank.
- * @return mixed Rank value
- */
- function sogou()
- {
- $result = $this->fetch('http://www.sogou.com/web?query=link%3A' . urlencode($this->_domain));
- if(preg_match('/<\/span>(\d+)<\/dd>/', $result, $match)) {
- return $match[1];
- } else {
- $this->_error = 'Failed to fetch sogou rank!';
- return 0;
- }
- }
-
- // }}}
-
- // {{{ chinarank()
-
- /**
- * Fetch the ChinaRank rank.
- * @return mixed Rank value
- */
- function chinarank()
- {
- list($msec, $sec) = split(' ', microtime());
- $r = $sec . substr($msec, 2, 3);
- $result = $this->fetch('http://www.chinarank.org.cn/overview/Info.do?url=' . urlencode($this->_domain) . '&r=' . $r);
- if(preg_match('/class=\"domain\">(\d+)<\/span>/', $result, $match)) {
- return $match[1];
- } else {
- $this->_error = 'Failed to fetch chinarank!';
- return 0;
- }
- }
-
- // }}}
-
- // {{{ fetch()
-
- /**
- * Fetch the remote content.
- * @param string URL of the page
- * @return string Content of the page
- */
- function fetch($url)
- {
- $content = '';
- if(function_exists('curl_init')) {
- //Curl Method
- $handle = curl_init();
- if(!$handle) {
- return false;
- }
- curl_setopt($handle, CURLOPT_URL, $url);
- curl_setopt($handle, CURLOPT_CONNECTTIMEOUT, 5);
- curl_setopt($handle, CURLOPT_RETURNTRANSFER, 1);
- curl_setopt($handle, CURLOPT_FOLLOWLOCATION, 0);
- $content = curl_exec($handle);
- curl_close($handle);
- } elseif(ini_get('allow_url_fopen')) {
- //File Method
- $handle = @fopen($url, 'r');
- if(!$handle) {
- return false;
- }
- while($buffer = fgets($handle, 4096)) {
- $content .= $buffer;
- }
- fclose($handle);
- } elseif(function_exists('fsockopen')) {
- //Socket Method
- $pos = strpos($url, '://');
- $host = substr($url, $pos + 3, strpos($url, '/', $pos + 3) - $pos - 3);
- $uri = substr($url, strpos($url, '/', $pos + 3));
- $request = "GET " . $uri . " HTTP/1.1\r\n"
- . "Host: " . $host . "\r\n"
- . "Accept: */*\r\n"
- . "User-Agent: Mozilla/4.0 (compatible; MSIE 5.5; Windows 98)\r\n"
- . "\r\n";
- $handle = @fsockopen($host, 80, $errno, $errstr, 30);
- if(!$handle) {
- return false;
- }
- @fputs($handle, $request);
- while(!feof($handle)) {
- $content .= fgets($handle, 4096);
- }
- fclose($handle);
- $separator = strpos($content, "\r\n\r\n");
- if($separator === false) {
- return $content;
- } else {
- return substr($content, $separator + 4);
- }
- }
- return $content;
- }
-
- // }}}
-
- // {{{ strord()
-
- function strord($string)
- {
- $strlen = strlen($string);
- for($i = 0; $i < $strlen; $i++) {
- $result[$i] = ord($string{$i});
- }
- return $result;
- }
-
- // }}}
-
- // {{{ GCH()
-
- function GCH($url, $length=null)
- {
- $length = sizeof($url);
- $a = $b = 0x9E3779B9;
- $c = 0xE6359A60;
- $k = 0;
- $len = $length;
- while($len >= 12) {
- $a += ($url[$k + 0] + ($url[$k + 1] << 8) + ($url[$k + 2] << 16) + ($url[$k + 3] << 24));
- $b += ($url[$k + 4] + ($url[$k + 5] << 8) + ($url[$k + 6] << 16) + ($url[$k + 7] << 24));
- $c += ($url[$k + 8] + ($url[$k + 9] << 8) + ($url[$k + 10] << 16) + ($url[$k + 11] << 24));
- $mix = $this->mix($a, $b, $c);
- $a = $mix[0];
- $b = $mix[1];
- $c = $mix[2];
- $k += 12;
- $len -= 12;
- }
- $c += $length;
- //All the case statements fall through
- switch($len) {
- case 11: $c += ($url[$k + 10] << 24);
- case 10: $c += ($url[$k + 9] << 16);
- case 9 : $c += ($url[$k + 8] << 8);
- //The first byte of c is reserved for the length
- case 8 : $b += ($url[$k + 7] << 24);
- case 7 : $b += ($url[$k + 6] << 16);
- case 6 : $b += ($url[$k + 5] << 8);
- case 5 : $b += ($url[$k + 4]);
- case 4 : $a += ($url[$k + 3] << 24);
- case 3 : $a += ($url[$k + 2] << 16);
- case 2 : $a += ($url[$k + 1] << 8);
- case 1 : $a += ($url[$k + 0]);
- //Case 0: nothing left to add
- }
- $mix = $this->mix($a, $b, $c);
- //Report the result
- return $mix[2];
- }
-
- // }}}
-
- // {{{ mix()
-
- function mix($a, $b, $c){
- $a -= $b;
- $a -= $c;
- $a ^= ($this->zeroFill($c, 13));
- $b -= $c;
- $b -= $a;
- $b ^= ($a << 8);
- $c -= $a;
- $c -= $b;
- $c ^= ($this->zeroFill($b, 13));
- $a -= $b;
- $a -= $c;
- $a ^= ($this->zeroFill($c, 12));
- $b -= $c;
- $b -= $a;
- $b ^= ($a << 16);
- $c -= $a;
- $c -= $b;
- $c ^= ($this->zeroFill($b, 5));
- $a -= $b;
- $a -= $c;
- $a ^= ($this->zeroFill($c, 3));
- $b -= $c;
- $b -= $a;
- $b ^= ($a << 10);
- $c -= $a;
- $c -= $b;
- $c ^= ($this->zeroFill($b, 15));
- return array($a, $b, $c);
- }
-
- // }}}
-
- // {{{ zeroFill()
-
- function zeroFill($a, $b)
- {
- $z = hexdec(80000000);
- if($z & $a) {
- $a = ($a >> 1);
- $a &= (~ $z);
- $a |= 0x40000000;
- $a = ($a >> ($b - 1));
- } else{
- $a = ($a>>$b);
- }
- return $a;
- }
-
- // }}}
-
- }
-
- ?>
复制代码- < ?php
- include './class_rank.php';
- // set up your domain
- $rank = new Rank('www.quchao.com');
- // output the alexa rank
- echo $rank->process('alexa');
- // output the google page rank
- echo $rank->process('google');
- // output the sogou page rank
- echo $rank->process('sogou');
- // output the chinarank
- echo $rank->process('chinarank');
- ?>
复制代码 |
|