2010년 8월 25일 수요일

[프로그래밍] PHP Alexa Rank Fetcher Class


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





  1. /* the alexa rank class */  

  2. class alexa   

  3. {   

  4.     /* initial vars */  

  5.     var $xml;   

  6.     var $values;   

  7.     var $alexa_address;   

  8.   

  9.     /* the constructor */  

  10.     function alexa($alexa_address,$domain)   

  11.     {   

  12.         $this->alexa_address = $alexa_address;   

  13.         $this->xml = $this->get_data($domain);   

  14.         $this->set();   

  15.     }   

  16.   

  17.     /* gets the xml data from Alexa */  

  18.     function get_data($domain)   

  19.     {   

  20.         $url = $this->alexa_address.'http://'.$domain;   

  21.         $xml = file_get_contents($url);   

  22.         return $xml;   

  23.     }   

  24.   

  25.     /* set values in the XML that we want */  

  26.     function set()   

  27.     {   

  28.         $this->values['rank'] = (preg_match('/POPULARITY URL="[a-z0-9-./]{1,}" TEXT="([0-9]{1,12})"/',$this->xml,$regs) ? number_format($regs[1]) : 0);   

  29.         $this->values['reach'] = (preg_match('/REACH RANK="([0-9]{1,12})"/',$this->xml,$regs) ? number_format($regs[1]) : 0);   

  30.         $this->values['linksin'] = (preg_match('/LINKSIN NUM="([0-9]{1,12})"/',$this->xml,$regs) ? number_format($regs[1]) : 0);   

  31.     }   

  32.   

  33.     /* returns the requested value */  

  34.     function get($value)   

  35.     {   

  36.         return (isset($this->values[$value]) ? $this->values[$value] : '"'.$value.'" does not exist.');   

  37.     }   

  38. }  

The Code - PHP5 Version





  1. /* the alexa rank class */  

  2. class alexa   

  3. {   

  4.     /* initial vars */  

  5.     var $xml;   

  6.     var $values;   

  7.     var $alexa_address;   

  8.   

  9.     /* the constructor */  

  10.     function alexa($alexa_address,$domain)   

  11.     {   

  12.         $this->alexa_address = $alexa_address;   

  13.         $this->xml = $this->get_data($domain);   

  14.         $this->set();   

  15.     }   

  16.   

  17.     /* gets the xml data from Alexa */  

  18.     function get_data($domain)   

  19.     {   

  20.         $url = $this->alexa_address.'http://'.$domain;   

  21.         $xml = simplexml_load_file($urlor die('Cannot retrieve feed');   

  22.         return $xml;   

  23.     }   

  24.   

  25.     /* set values in the XML that we want */  

  26.     function set()   

  27.     {   

  28.         $this->values['rank'] = ($this->xml->SD->POPULARITY['TEXT'] ? number_format($this->xml->SD->POPULARITY['TEXT']) : 0);   

  29.         $this->values['reach'] = ($this->xml->SD->REACH['RANK'] ? number_format($this->xml->SD->REACH['RANK']) : 0);   

  30.         $this->values['linksin'] = ($this->xml->SD->LINKSIN['NUM'] ? number_format($this->xml->SD->LINKSIN['NUM']) : 0);   

  31.     }   

  32.   

  33.     /* returns the requested value */  

  34.     function get($value)   

  35.     {   

  36.         return (isset($this->values[$value]) ? $this->values[$value] : '"'.$value.'" does not exist.');   

  37.     }   

  38. }  

Using CURL


If you'd rather use the CURL library, you can simply modify the get_data() function:





  1. /* gets the xml data from Alexa */  

  2. function get_data($domain)   

  3. {   

  4.     $url = $this->alexa_address.'http://'.$domain;   

  5.     $ch = curl_init();   

  6.     $timeout = 5;   

  7.     curl_setopt($ch,CURLOPT_URL,$url);   

  8.     curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);   

  9.     curl_setopt($ch,CURLOPT_CONNECTTIMEOUT,$timeout);   

  10.     $xml = curl_exec($ch);   

  11.     curl_close($ch);   

  12.     return $xml;   

  13. }  

The Usage


Provide two paramenters: the path to the XML file (minus the domain) and the domain.





  1. /* retrieve & display rank */  

  2. $alexa_connector = new alexa('http://alexa.com/xml/dad?url=','digg.com'); // domain only!   

  3. echo 'Rank :: '.$alexa_connector->get('rank'); // returns 118   

  4. echo '';   

  5. echo 'Reach :: '.$alexa_connector->get('reach'); // returns 95   

  6. echo '';   

  7. echo 'Links In :: '.$alexa_connector->get('linksin'); // returns 34,414  

댓글 없음:

댓글 쓰기