PHP Alexa Rank类

php类获取Alexa排名,获取以下相关信息:

  1. 人气排名
  2. 日访问量
  3. 反向链接
  4. 响应速度

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

“PHP Alexa Rank类”的10个回复

          1. 谢谢耐心回答,我以以下代码用访问,也不行,是否现在不能用了呢?

            <?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 */
            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.’);
            }
            }

            /* 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
            ?>

  1. 我用你这个类为什么报错啊Cannot retrieve feed 这是怎么回事啊 我想批量采集网站的日ip量

发表评论

电子邮件地址不会被公开。 必填项已用*标注