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.
- 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.
- 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.
-
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.'); } } } ?> -
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. - You can have a look at the Demo in Here.
- Also you can download the code snippet from Here.
- Please feel free to extend and modify the codes. Also please let me know if more can be done on this.
Comments
Gwtting web traffic stats images from alexa.
Submitted by noren (not verified) on
Hi.. how can i get traffic stats images for a domain in proper way...i can get these images using direct URLs like 'http://traffic.alexa.com/graph?w=550&h=300&o=f&c=1&y=t&b=ffffff&r=6m&u=r...'
is it a secure way ??? or should i get these images writing code using APIs
Using 3rd Party API
Submitted by aneek on
Dear Noren,
I think you can show up images for the graphs by using APIs. But I guess alexa doesn't provide any APIs for this.
So you can follow this one. https://github.com/eyecatchup/SEOstats
In the SEOstats / examples directory you will find an example as example-5.php. In this one you will see they used the Alexa Class to retrieve values from Alexa website. It does require cURL. Now if you write like this below code you will get the Graph output.
<?php try { $url = new SEOstats('rediff.com'); echo $url->Alexa_Graph(); //var_dump( $url->Alexa_Graph()); } ?>You can follow the documentation guide here : http://code.google.com/p/seostats/#How_To_Use
In Java
Submitted by ocpjp (not verified) on
Do you any examples for doing the same using Java?
No clues,
Submitted by aneek on
I do not have any idea about doing this in Java though, but you can google to find it out. Because the functions will remain the same I hope.
Thanks buddy
Submitted by Rashod (not verified) on
I was looking some thing like this for long time.I am developing a social bookmarking web site .So I wanted to show alexa rank for every link sheared in network.
Thank You
my social network www.myshouter.com
Add new comment