PHP Script: Alexa Traffic Rank
June 12th, 2007 by Matt HugginsI’m currently developing a project that I have yet to announce here. In the process, I recognized the need to collect Alexa traffic rank data. Amazon.com, the owning company, offers the opportunity to download traffic rank data for a cost. However, I decided I wanted this data for free since it’s displayed on their website anyway.
I searched for PHP scripts to accomplish the task of retrieving the Alexa traffic rank, and the closest thing I could find was a script compiled by Webdigity. I soon found that Alexa implemented a scrambling mechanism meaning that this script could not be used.
I decided to take the bit of code that Webdigity’s script offered, and I made it more robust to work around Alexa’s screen scraping prevention system. The system currently in place simply takes a rank number (e.g. 230,829) and splits the digits up among several SPAN elements. Additionally, several decoy digits are inserted on SPAN elements with a random class name whose class has a “display:none” CSS attribute. The result is a chunk of code that looks something like this:
<span class="c8be">23</span><span class="c2bd">0,</span><span class="cb79">38</span><span class="c65a">89</span><span class="cae7">29</span>2</span>
My Alexa script will retrieve the list of decoy CSS classes. It then compares these to the HTML elements to determine which ones are hidden, and it removes them. All extraneous HTML and non-numeric characters are stripped, and the Alexa traffic rank is returned. Below is an example of how easy this code is to use.
include('class.alexa.php');
$alexa = new Alexa();
$rank = $alexa->getRank('www.matthuggins.com');
Download the Alexa Traffic Rank PHP Script
Did you find this script useful? PayPal donations are greatly appreciated!
Related Posts:

Subscribe by E-mail
June 12th, 2007 at 11:30 am
I wonder if you can help to hack this code for use on blogspot blogs.
June 12th, 2007 at 12:41 pm
As far as I’m aware, Blogger doesn’t allows PHP code manipulation. It might be able to be done in Javascript though.
August 16th, 2007 at 10:24 pm
I have been using this class for several weeks now and it appears to have stopped working today. I have a fair bit of knowledge of PHP, but curl is new to me so I don’t really know how to debug that part of the class. Has Amazon changed something or is it possible that my server IP has been banned?
Either way, thanks for sharing it with us!
August 16th, 2007 at 11:27 pm
Thanks for the heads-up on this. It looks like Alexa changed the output on their traffic rank HTML, and since this is just a screen-scraper, it’s not deciphering the value correctly. I’ll have to update this ASAP, I’ll let you know when I do!
November 1st, 2007 at 12:08 pm
Is this script still functional?
Thanks
December 19th, 2007 at 6:26 am
A low ranking on Alexa does not indicate that the traffic is not being generated on http://www.google.co.in, http://www.search.yahoo.com and http://www.search.msn.com search engines.
July 5th, 2008 at 12:49 am
very nice script………
thx a lot
July 5th, 2008 at 12:54 am
wht happen if alexa changes the design source?
is there any script that provide reliable traffic rank?
plz let me know
thanks in advance
keyur