Get Alexa Rank with PHP and Alexa API

Category: 

Alexa, we use this website to check the traffic rank to our site. For that we have to go to alexa.com and then check by the domain names. But what if we make a widget and place it on our site to check Alexa ranks. Actually, the main goal to publish this article is to show how we can use PHP code to get Alexa Rank through Alexa XML API. So without further more delay lets go into the coding.

  1. We are going to Use a XML parser Class to parse the XML returned by Alexa. Also we will make a class for outselves and we will call it alexa.class.php.
  2. From Alexa documentation we get an URL as http://data.alexa.com/data?cli=10&dat=snbamz&url=http://(only_domain_name). So we will use this one to generate our XML.
  3. So let us write the alexa class. Remember this class will take help form the XML Parser class.
     <?php
    require_once 'ParseXml.class.php';
     
    class AlexaRank extends ParseXml{
    	private $data;
    	private $values = array();
     
    	function __construct($domain) {
    		$this->data = $this->fetchData($domain);
    		$this->findValue();
    	}
     
    	private function fetchData($domain) {
    		$url = "http://data.alexa.com/data?cli=10&dat=snbamz&url=http://".trim($domain);
    		$this->LoadRemote($url, 5);
    		$dataArray = $this->ToArray();
    		return $dataArray;
    	}
     
    	private function findValue() {
    		$this->values = array(
    			'rank' => (isset($this->data['SD'][1]['POPULARITY']['@attributes']['TEXT']) ? ($this->data['SD'][1]['POPULARITY']['@attributes']['TEXT']) : NULL),
    			'created' => (isset($this->data['SD'][0]['CREATED']['@attributes']['DATE']) ? ($this->data['SD'][0]['CREATED']['@attributes']['DATE']) : NULL),
    			'email' => (isset($this->data['SD'][0]['EMAIL']['@attributes']['ADDR']) ? ($this->data['SD'][0]['EMAIL']['@attributes']['ADDR']) : NULL),
    			'linksin' => (isset($this->data['SD'][0]['LINKSIN']['@attributes']['NUM']) ? ($this->data['SD'][0]['LINKSIN']['@attributes']['NUM']) : NULL),
    			'reach' => (isset($this->data['SD'][1]['REACH']['@attributes']['RANK']) ? ($this->data['SD'][1]['REACH']['@attributes']['RANK']) : NULL),
    			'baseuri' => (isset($this->data['DMOZ']['SITE']['@attributes']['BASE']) ? ($this->data['DMOZ']['SITE']['@attributes']['BASE']) : NULL),
    			'title' => (isset($this->data['DMOZ']['SITE']['@attributes']['TITLE']) ? ($this->data['DMOZ']['SITE']['@attributes']['TITLE']) : NULL),
    			'description' => (isset($this->data['DMOZ']['SITE']['@attributes']['DESC']) ? ($this->data['DMOZ']['SITE']['@attributes']['DESC']) : NULL),
    		);
    	}
     
    	public function get($value = NULL){
    		if($value === NULL) {
    			return $this->values; //Return the total Array
    		} else {
    			return (isset($this->values[$value]) ? $this->values[$value] : '"'.$value.'" does not exist.');
    		}	
    	}
     
    }
    ?>
  4. As, you can see that we are extending the AlexaRank class by ParseXml class, so to we will inherit the method properties in the AlexaRank class. First I have used a private method to get the XML data from the Alexa URL. It returns the XML format, then I have used the XML parser class's LoadRemote($url, 5); method where, LoadRemote('the url', 'timeout');.
    Then, ToArray() method converts the XML object to Array. So we have a working array. Now by the findValue() and get() method we are able to access the value.
  5. You can have a look at the Demo in Here.
  6. Also you can download the code snippet from Here.
  7. Please feel free to extend and modify the codes. Also please let me know if more can be done on this.

Rate this content: 

Average: 4.1 (27 votes)

Comments

Add new comment