返回列表 回复 发帖

PHP Alexa Rank类

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

人气排名

日访问量

反向链接

响应速度

PHP4版本代码

  1. /* the alexa rank class */
  2. class alexa
  3. {
  4. /* initial vars */
  5. var $xml;
  6. var $values;
  7. var $alexa_address;
  8. /* the constructor */
  9. function alexa($alexa_address,$domain)
  10. {
  11. $this->alexa_address = $alexa_address;
  12. $this->xml = $this->get_data($domain);
  13. $this->set();
  14. }
  15. /* gets the xml data from Alexa */
  16. function get_data($domain)
  17. {
  18. $url = $this->alexa_address.'http://'.$domain;
  19. $xml = file_get_contents($url);
  20. return $xml;
  21. }
  22. /* set values in the XML that we want */
  23. function set()
  24. {
  25. $this->values['rank'] = (preg_match('/POPULARITY URL="[a-z0-9\\-\\.\\/]{1,}" TEXT="([0-9]{1,12})"/',$this->xml,$regs) ? number_format($regs[1]) : 0);
  26. $this->values['reach'] = (preg_match('/REACH RANK="([0-9]{1,12})"/',$this->xml,$regs) ? number_format($regs[1]) : 0);
  27. $this->values['linksin'] = (preg_match('/LINKSIN NUM="([0-9]{1,12})"/',$this->xml,$regs) ? number_format($regs[1]) : 0);
  28. }
  29. /* returns the requested value */
  30. function get($value)
  31. {
  32. return (isset($this->values[$value]) ? $this->values[$value] : '"'.$value.'" does not exist.');
  33. }
  34. }
复制代码
PHP5版本代码

  1. /* the alexa rank class */
  2. class alexa
  3. {
  4. /* initial vars */
  5. var $xml;
  6. var $values;
  7. var $alexa_address;
  8. /* the constructor */
  9. function alexa($alexa_address,$domain)
  10. {
  11. $this->alexa_address = $alexa_address;
  12. $this->xml = $this->get_data($domain);
  13. $this->set();
  14. }
  15. /* gets the xml data from Alexa */
  16. function get_data($domain)
  17. {
  18. $url = $this->alexa_address.'http://'.$domain;
  19. $xml = simplexml_load_file($url) or die('Cannot retrieve feed');
  20. return $xml;
  21. }
  22. /* set values in the XML that we want */
  23. function set()
  24. {
  25. $this->values['rank'] = ($this->xml->SD->POPULARITY['TEXT'] ? number_format($this->xml->SD->POPULARITY['TEXT']) : 0);
  26. $this->values['reach'] = ($this->xml->SD->REACH['RANK'] ? number_format($this->xml->SD->REACH['RANK']) : 0);
  27. $this->values['linksin'] = ($this->xml->SD->LINKSIN['NUM'] ? number_format($this->xml->SD->LINKSIN['NUM']) : 0);
  28. }
  29. /* returns the requested value */
  30. function get($value)
  31. {
  32. return (isset($this->values[$value]) ? $this->values[$value] : '"'.$value.'" does not exist.');
  33. }
  34. }
复制代码
使用cURL
如果您使用curl库,你可以简单地修改get_data()函数:

  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. }
复制代码
用法:

  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
复制代码
返回列表