PHP: Displaying the Alexa ranking of a web site

Alexa is a subsidiary of Amazon. They gather data about web sites and offer web traffic metrics. What’s called the Alexa Rank is basically the position of a web site compared to all websites.

There is no real public API provided to get information from Alexa. But there’s a URL where you can get an XML response for a given URL. The response will then contain some useful information (the rank among others).

The URL goes like this:

http://data.alexa.com/data?cli=10&url=<THE_URL_TO_CHECK>

Here an example from the command line:

$ curl "http://data.alexa.com/data?cli=10&url=https://benohead.com"
<?xml version="1.0" encoding="UTF-8"?>

<ALEXA VER="0.9" URL="benohead.com/" HOME="0" AID="=">
<DMOZ>
<SITE BASE="benohead.com/" TITLE="Benohead's Software Blog" DESC="A blog about software related topics: Linux, Mac OS X, Java, HL7, DICOM, WordPress, web design, databases...">
<CATS/>
</SITE>
</DMOZ>
<SD>
<POPULARITY URL="benohead.com/" TEXT="2428475" SOURCE="panel"/>
<REACH RANK="2070623"/>
<RANK DELTA="-19485883"/>
<COUNTRY CODE="US" NAME="United States" RANK="1914521"/>
</SD>
</ALEXA>

If you go to alexa.com and search for benohead.com, you’ll see that 2428475 is the Alexa Rank of this page. So we just need to extract this info using PHP:

First get the data from the URL for a given domain:

$data = file_get_contents("http://xml.alexa.com/data?cli=10&url=".$domain);

Now the data you got was a XML message. So you’ll some XML library to fully interpret it. But since we’re only really interested in the rank, we can just match the string using preg_match:

if (preg_match('/\<popularity url\="(.*?)" text\="([0-9]+)" source="(.*?)"\/\>/si', $data, $matches)) 
{ 
   $rank = $matches[2]; 
}

If you are also interested in the number of links, you will need to add dat=snbamz to the URL call:

$data = file_get_contents("http://xml.alexa.com/data?cli=10&dat=snbamz&url=".$domain);

This will return something like this:

<?xml version="1.0" encoding="UTF-8"?>

<ALEXA VER="0.9" URL="benohead.com/" HOME="0" AID="=">
<RLS PREFIX="http://" more="0">
</RLS>
<SD TITLE="A" FLAGS="DMOZ" HOST="benohead.com">
<CLAIMED DATE="2012-06-26T16:26:04Z"/>
<COUNTRY CODE="US"/>
<ALEXAPRO TIER="intro"/>
<LINKSIN NUM="11"/>
</SD>
<DMOZ>
<SITE BASE="benohead.com/" TITLE="Benohead's Software Blog" DESC="A blog about software related topics: Linux, Mac OS X, Java, HL7, DICOM, WordPress, web design, databases...">
<CATS/>
</SITE>
</DMOZ>
<SD>
<POPULARITY URL="benohead.com/" TEXT="2428475" SOURCE="panel"/>
<REACH RANK="2070623"/>
<RANK DELTA="-19485883"/>
<COUNTRY CODE="US" NAME="United States" RANK="1914521"/>
</SD>
</ALEXA>

The LINKSIN NUM part contains the number of incoming links. So you can fetch it like this:

if (preg_match('/\<linksin num\="([0-9]+)"\/\>/si', $data, $matches))
{
   $nooflinks = $matches[1];
}

Putting it all together:

<?php
$domain = 'benohead.com';
echo 'Domain:'.$domain.'<br>';
$data = file_get_contents('http://data.alexa.com/data?cli=10&dat=snbamz&url='.$domain);
if (preg_match('/\<popularity url\="(.*?)" text\="([0-9]+)" source="(.*?)"\/\>/si', $data, $matches))
{
    $rank = $matches[2];
}
if (preg_match('/\<linksin num\="([0-9]+)"\/\>/si', $data, $matches))
{
   $nooflinks = $matches[1];
}
echo 'Alexa Rank:'.number_format($rank).'<br>';
echo 'Number of links:'.number_format($nooflinks);
?>

5 thoughts on “PHP: Displaying the Alexa ranking of a web site

  1. Thank you for such a nice php script. I do have a question however.

    If I were to display, say five different sites using the above php script & if I wanted to put them in order of their respective Alexa rank, how would I go about doing that?

    For instance, if I had a table displaying these five sites, how can I automatically place the highest ranking site on top?

    Thanks.

    1. The country rank is also contained in the returned XML i.e. for spiegel.de, you get:

      <SD>
      <POPULARITY URL="spiegel.de/" TEXT="205" SOURCE="panel"/>
      <REACH RANK="230"/>
      <RANK DELTA="-161"/>
      <COUNTRY CODE="DE" NAME="Germany" RANK="8"/>
      </SD>

      205 is the Global Rank, -161 means the rank was 366 three months ago and 8 is the Rank in Germany.

Leave a Reply

Your email address will not be published. Required fields are marked *