PHP Alexa Rank Fetcher Class
The most popular online website popularity measuring stick appears to be Alexa. Alexa provides a wealth of information on a given website, most notably:
- Popularity rank (the most important one)
- Reach
- In-links
- Speed
Alexa provides this information in many useful formats, including XML. Using the XML provided by Alexa, we can gain access to Alexa information within our pages. I've created a PHP class to make fetching Alexa data free, quick, and easy. The class comes in a PHP4 version and a PHP5 version.
The Code - PHP4 Version
- /* 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.');
- }
- }
/* 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.');
}
}
The Code - PHP5 Version
- /* 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.');
- }
- }
/* 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.');
}
}
Using CURL
If you'd rather use the CURL library, you can simply modify the get_data() function:
- /* 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;
- }
/* 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;
}
The Usage
Provide two paramenters: the path to the XML file (minus the domain) and the domain.
- /* 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
댓글 없음:
댓글 쓰기