Alexa

How to retrieve the Alexa Traffic Rank of any domain

After my post on using PHP to retrieve the Google Pagerank of any domain I decided to share my method of retrieving the Alexa Traffic Rank for any domain with you.

The Alexa Traffic Rank is based on how many users of their Alexa toolbar visit a certain website. There is some controversy over how representative Alexa’s user base is of typical Internet behavior, especially for less trafficked sites but nonetheless it’s a pretty good indication on how popular a website is.

Alexa offers a, somewhat messy, XML feed featuring a lot of data on a certain domain so getting the Alexa Traffic Rank is relatively easy. I use the following PHP class:

<?php

Class Alexa {

	function getAlexaRank($domain)
	{
		$remote_url = 'http://data.alexa.com/data?cli=10&dat=snbamz&url=' . trim($domain);
		$xml = simplexml_load_file($remote_url);
		if(isset($xml->SD[1]->POPULARITY['TEXT'])){
			return number_format($xml->SD[1]->POPULARITY['TEXT']);
		} else {
			return 0;
		}
	}
}

After that you can simply use the following code to get the Alexa Traffic Rank.

<?php

$alexa = new Alexa;
$alexarank = $alexa->getAlexaRank('http://www.domain.com');

Please note that if Alexa doesn’t have any data on your domain it will return a Traffic Rank of 0.