<?
/* 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.');
}
}
/* 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=','otelreferans.com'); // domain only!
echo 'Rank :: '.$alexa_connector->get('rank');
echo '';
echo 'Reach :: '.$alexa_connector->get('reach');
echo '';
echo 'Links In :: '.$alexa_connector->get('linksin');
?>