PHP Script: Alexa Traffic Rank

June 12th, 2007 by Matt Huggins

If you're new here, you may want to subscribe to my RSS feed. Thanks for visiting!

I’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:

8 Responses to “PHP Script: Alexa Traffic Rank”

  1. betshopboy Says:

    I wonder if you can help to hack this code for use on blogspot blogs.

  2. Matt Huggins Says:

    As far as I’m aware, Blogger doesn’t allows PHP code manipulation. It might be able to be done in Javascript though.

  3. Search Marketing Sense Says:

    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!

  4. Matt Huggins Says:

    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!

  5. Andre Says:

    Is this script still functional?
    Thanks

  6. Alexa ranking affects SERP ranking? Says:

    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.

  7. keyur Says:

    very nice script………
    thx a lot
    :)

  8. keyur Says:

    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


Rodney's Kontera DynamiContext Plugin plugged in.