php类获取Alexa排名,获取以下相关信息:
- 人气排名
- 日访问量
- 反向链接
- 响应速度
PHP4版本代码
/* the alexa rank class */ class alexa { /* initial vars */ var $xml; var $values; var $alexa_address; /* the constructor */ function alexa($alexa_address,$domain) { $this->alexa_address = $alexa_address; $this->xml = $this->get_data($domain); $this->set(); } /* gets the xml data from Alexa */ function get_data($domain) { $url = $this->alexa_address.'http://'.$domain; $xml = file_get_contents($url); return $xml; } /* set values in the XML that we want */ function set() { $this->values['rank'] = (preg_match('/POPULARITY URL="[a-z0-9\\-\\.\\/]{1,}" TEXT="([0-9]{1,12})"/',$this->xml,$regs) ? number_format($regs[1]) : 0); $this->values['reach'] = (preg_match('/REACH RANK="([0-9]{1,12})"/',$this->xml,$regs) ? number_format($regs[1]) : 0); $this->values['linksin'] = (preg_match('/LINKSIN NUM="([0-9]{1,12})"/',$this->xml,$regs) ? number_format($regs[1]) : 0); } /* returns the requested value */ function get($value) { return (isset($this->values[$value]) ? $this->values[$value] : '"'.$value.'" does not exist.'); } }
PHP5版本代码
/* the alexa rank class */ class alexa { /* initial vars */ var $xml; var $values; var $alexa_address; /* the constructor */ function alexa($alexa_address,$domain) { $this->alexa_address = $alexa_address; $this->xml = $this->get_data($domain); $this->set(); } /* gets the xml data from Alexa */ function get_data($domain) { $url = $this->alexa_address.'http://'.$domain; $xml = simplexml_load_file($url) or die('Cannot retrieve feed'); return $xml; } /* set values in the XML that we want */ function set() { $this->values['rank'] = ($this->xml->SD->POPULARITY['TEXT'] ? number_format($this->xml->SD->POPULARITY['TEXT']) : 0); $this->values['reach'] = ($this->xml->SD->REACH['RANK'] ? number_format($this->xml->SD->REACH['RANK']) : 0); $this->values['linksin'] = ($this->xml->SD->LINKSIN['NUM'] ? number_format($this->xml->SD->LINKSIN['NUM']) : 0); } /* returns the requested value */ function get($value) { return (isset($this->values[$value]) ? $this->values[$value] : '"'.$value.'" does not exist.'); } }
使用cURL
如果您使用curl库,你可以简单地修改get_data()函数:
/* gets the XML data from Alexa */ function get_data($domain) { $url = $this->alexa_address.'http://'.$domain; $ch = curl_init(); $timeout = 5; curl_setopt($ch,CURLOPT_URL,$url); curl_setopt($ch,CURLOPT_RETURNTRANSFER,1); curl_setopt($ch,CURLOPT_CONNECTTIMEOUT,$timeout); $xml = curl_exec($ch); curl_close($ch); return $xml; }
用法:
/* retrieve & display rank */ $alexa_connector = new alexa('http://alexa.com/xml/dad?url=','digg.com'); // domain only! echo 'Rank :: '.$alexa_connector->get('rank'); // returns 118 echo ''; echo 'Reach :: '.$alexa_connector->get('reach'); // returns 95 echo ''; echo 'Links In :: '.$alexa_connector->get('linksin'); // returns 34,414
这个还有用吗?
这个网址是否对?
http://alexa.com/xml/dad?url=
不存在了哦。
可以用的,但是不能直接打开。有加密算法
我直接用,用不了,有什么其它门道吗?
http://alexa.com/xml/dad?url=digg.com的结果是:
Moved Permanently
The document has moved here.
直接不能用 要用这个类来获取
谢谢耐心回答,我以以下代码用访问,也不行,是否现在不能用了呢?
<?php
/* the alexa rank class */
class alexa
{
/* initial vars */
var $xml;
var $values;
var $alexa_address;
/* the constructor */
function alexa($alexa_address,$domain)
{
$this->alexa_address = $alexa_address;
$this->xml = $this->get_data($domain);
$this->set();
}
/* gets the xml data from Alexa */
/* gets the XML data from Alexa */
function get_data($domain)
{
$url = $this->alexa_address.’http://’.$domain;
$ch = curl_init();
$timeout = 5;
curl_setopt($ch,CURLOPT_URL,$url);
curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch,CURLOPT_CONNECTTIMEOUT,$timeout);
$xml = curl_exec($ch);
curl_close($ch);
return $xml;
}
/* set values in the XML that we want */
) : 0);
) : 0);
) : 0);
function set()
{
$this->values[‘rank’] = (preg_match(‘/POPULARITY URL=”[a-z0-9\-\.\/]{1,}” TEXT=”([0-9]{1,12})”/’,$this->xml,$regs) ? number_format($regs
$this->values[‘reach’] = (preg_match(‘/REACH RANK=”([0-9]{1,12})”/’,$this->xml,$regs) ? number_format($regs
$this->values[‘linksin’] = (preg_match(‘/LINKSIN NUM=”([0-9]{1,12})”/’,$this->xml,$regs) ? number_format($regs
}
/* returns the requested value */
function get($value)
{
return (isset($this->values[$value]) ? $this->values[$value] : ‘”‘.$value.'” does not exist.’);
}
}
/* retrieve & display rank */
$alexa_connector = new alexa(‘http://alexa.com/xml/dad?url=‘,’digg.com’); // domain only!
echo ‘Rank :: ‘.$alexa_connector->get(‘rank’); // returns 118
echo ”;
echo ‘Reach :: ‘.$alexa_connector->get(‘reach’); // returns 95
echo ”;
echo ‘Links In :: ‘.$alexa_connector->get(‘linksin’); // returns 34,414
?>
我中午回去 再发一遍给你。里面有这个方法
改天试试。
我用你这个类为什么报错啊Cannot retrieve feed 这是怎么回事啊 我想批量采集网站的日ip量
我用的没问题
这的东西一直很实用,博主大才!