There are several posts out there to get Alexa traffic rank using PHP. In fact, few months back I published an article on how to get Alexa traffic rank using PHP. In this post, I am going to expand the code to include more Alexa stats such as Country Rank and Reach Rank. Getting these details on your site or your competitors site and recording them in a database can be a valuable tool in analyzing your site's progress and comparing it with your competitor's. You can also measure the impact of a recent change you made on your site. Without further delay, let us look at how to get Alexa traffic rank and popularity for any site using PHP.
Alexa Traffic Statistics
Many advertisers and brokers use Alexa statistics to gauge that value of your site before bidding for your ad slots. Some point out that Alexa statistics are collected only based on visitors with Alexa tool bar visiting a site and therefore is not representative of the whole population. While I tend to agree with this, current state of the situation is that, to generate revenue, one has to provide advertisers with what they want.
Get Alexa Traffic Rank and Popularity using PHP
Recording a daily snapshot of your statistics, in opinion, is a great way monitor your sites historical performance and plan future updates/upgrades. Using the PHP code presented in this guide, you can get Alexa Traffic Rank (both Global and Country) and popularity (Sites Linking In and Reach Rank). These numbers can be recorded in a database. You can even display as simple format text, which must faster than loading the Alexa widget.
To get Alexa traffic rank details, use the following PHP code.
Download the full code using the download button below:
The file_get_contents()
function gets the Alexa stats as a XML file. The reminder of the code breaks down the XML file, searches it, and gets the values we are interested in. cli=10
instructs Alexa to provide the results in XML format. dat=snbamz
part tells it to provide detailed statistics. Finally, url=htpcbeginner.com
is the part you want to edit. Change htpcbeginner.com
to the site you are interested in. The XML output is stored in variable $source
.
The Alexa Rank part of the code searches the XML out and matches the part that contains the Global Rank, which stored in the variable $aresult
. The Alexa Sites Linking in part, finds the number of links to your site from the XML output and stores it in the variable $alinksin
.
The Alexa US rank part finds the site's US rank and stores it in the variable $usrank
. If your site is most popular in other countries you can use the country name and its 2 letter abbreviation (example: IN
and India
). Carefully replace the words US
and United States
between the quotes in the code.
Finally, the reach rank is obtained from the XML output and stored in the variable $areach
.
Using the Data
You have managed to get Alexa traffic rank and popularity using PHP. So what do you do with it? Well, as I said before you could display it as simple text using the following PHP codes:
< ?php echo 'Alexa Traffic Rank: '.$aresult'.'<br />'; echo 'Alexa Sites Linking In: '.$alinksin.'<br />'; echo 'Alexa Country Rank: '.$usrank.'<br />'; echo 'Alexa Reach Rank: '.$areach; ?>
Remove the space between < and ? in the first line. This will output: Alexa Traffic Rank: 169481 Alexa Sites Linking In: 62 Alexa Country Rank: 38968 Alexa Reach Rank: 189529 Alternatively, you can record these numbers in a MySQL database. Periodically, you can plot the your historical Alexa statistics and monitoring your site's growth or performance. Trends in the graph show you the effect of something you just implemented. The example picture below show's htpcbeginner.com's historical Alexa Global Rank. The increase or rather deterioration in the rank in November 2012 was due to a change I made in the site. After noticing the drop in traffic and increase in ranking, I revoked all changes and then ranking improved again starting December 2012. Analyses like these can be very helpful and see how your site's progress is strending.
In addition to Alexa site statistics, you can also get Facebook likes count for your Fanpage and Twitter followers count using PHP. Go ahead and get Alexa traffic rank, popularity, and other interesting stats and use them creatively to understand and plan your sites progress.