PHP Alexa 排名获取器类

最著名的在线网站人气衡量标准似乎是 Alexa。 Alexa 在给定网站上提供了大量信息,最值得注意的是:

  • 人气排名(最重要的)
  • Reach
  • In-links
  • Speed

Alexa 以许多有用的格式(包括 XML)提供此信息。使用 Alexa 提供的 XML,我们可以在我们的页面中访问 Alexa 信息。我创建了一个 PHP 类来免费、快速、轻松地获取 Alexa 数据。该类有 PHP4 版本和 PHP5 版本。

代码 – 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 库,您可以简单地修改 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;
}

用法

提供两个参数: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

你对这门课有什么建议吗?

赞(0) 打赏

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

微信扫一扫打赏