Skip to the content...

Welcome to the David Walsh Blog. I'm a MooTools, Dojo, jQuery, CSS, and PHP Web Developer located in Madison, Wisconsin, United States. Please contact me if I can make your experience on my website better.

PHP Alexa Rank Fetcher Class

27 Responses »

The most well known online website popularity measuring stick appears to be Alexa. Alexa provides a wealth of information on a given website, most notably:

  • Popularity rank (the most important one)
  • Reach
  • In-links
  • Speed

Alexa provides this information in many useful formats, including XML. Using the XML provided by Alexa, we can gain access to Alexa information within our pages. I've created a PHP class to make fetching Alexa data free, quick, and easy. The class comes in a PHP4 version and a PHP5 version.

The Code - PHP4 Version

/* the alexa rank class */
class alexa
{
	/* initial vars */
	var $xml;
	var $values;
	var $alexa_address;

	/* the constructor */
	function alexa($alexa_address,$domain)
	{
		$this->alexa_address = $alexa_address;
		$this->xml = $this->get_data($domain);
		$this->set();
	}

	/* gets the xml data from Alexa */
	function get_data($domain)
	{
		$url = $this->alexa_address.'http://'.$domain;
		$xml = file_get_contents($url);
		return $xml;
	}

	/* set values in the XML that we want */
	function set()
	{
		$this->values['rank'] = (preg_match('/POPULARITY URL="[a-z0-9\\-\\.\\/]{1,}" TEXT="([0-9]{1,12})"/',$this->xml,$regs) ? number_format($regs[1]) : 0);
		$this->values['reach'] = (preg_match('/REACH RANK="([0-9]{1,12})"/',$this->xml,$regs) ? number_format($regs[1]) : 0);
		$this->values['linksin'] = (preg_match('/LINKSIN NUM="([0-9]{1,12})"/',$this->xml,$regs) ? number_format($regs[1]) : 0);
	}

	/* returns the requested value */
	function get($value)
	{
		return (isset($this->values[$value]) ? $this->values[$value] : '"'.$value.'" does not exist.');
	}
}

The Code - PHP5 Version

/* the alexa rank class */
class alexa
{
	/* initial vars */
	var $xml;
	var $values;
	var $alexa_address;

	/* the constructor */
	function alexa($alexa_address,$domain)
	{
		$this->alexa_address = $alexa_address;
		$this->xml = $this->get_data($domain);
		$this->set();
	}

	/* gets the xml data from Alexa */
	function get_data($domain)
	{
		$url = $this->alexa_address.'http://'.$domain;
		$xml = simplexml_load_file($url) or die('Cannot retrieve feed');
		return $xml;
	}

	/* set values in the XML that we want */
	function set()
	{
		$this->values['rank'] = ($this->xml->SD->POPULARITY['TEXT'] ? number_format($this->xml->SD->POPULARITY['TEXT']) : 0);
		$this->values['reach'] = ($this->xml->SD->REACH['RANK'] ? number_format($this->xml->SD->REACH['RANK']) : 0);
		$this->values['linksin'] = ($this->xml->SD->LINKSIN['NUM'] ? number_format($this->xml->SD->LINKSIN['NUM']) : 0);
	}

	/* returns the requested value */
	function get($value)
	{
		return (isset($this->values[$value]) ? $this->values[$value] : '"'.$value.'" does not exist.');
	}
}

Using cURL

If you'd rather use the cURL library, you can simply modify the get_data() function:

/* gets the XML data from Alexa */
function get_data($domain)
{
	$url = $this->alexa_address.'http://'.$domain;
	$ch = curl_init();
	$timeout = 5;
	curl_setopt($ch,CURLOPT_URL,$url);
	curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
	curl_setopt($ch,CURLOPT_CONNECTTIMEOUT,$timeout);
	$xml = curl_exec($ch);
	curl_close($ch);
	return $xml;
}

The Usage

Provide two paramenters: the path to the XML file (minus the domain) and the domain.

/* retrieve & display rank */
$alexa_connector = new alexa('http://alexa.com/xml/dad?url=','digg.com'); // domain only!
echo 'Rank :: '.$alexa_connector->get('rank'); // returns 118
echo '';
echo 'Reach :: '.$alexa_connector->get('reach'); // returns 95
echo '';
echo 'Links In :: '.$alexa_connector->get('linksin'); // returns 34,414

Do you have any suggestions for this class?

Discussion

  1. December 12, 2007 @ 11:00 am

    I did not know Alexia. It’s a good site. Thank’s for the suggest.

    About the class why not put the address site direct in the function get_data? If the address change you only need change in only place. In the constructor you need change in every call of this class.

  2. December 12, 2007 @ 12:40 pm

    I waffled between putting it inside the class or giving it to the constructor.

    In the end, I chose the constructor to make the class more “flexible,” so to speak. There are multiple URLs you can retrieve this information from.

  3. March 25, 2008 @ 1:26 am

    Hello …
    thank you for this script … but the result didn’t contain the alexa rank …

    it looks like :
    Rank :: 0
    Reach :: 745,382
    Links In :: 0

    any explain ….
    thanks anyway for your great effort

  4. March 25, 2008 @ 5:39 am

    Not all sites are given a rank Osama. What should it be?

  5. March 30, 2008 @ 10:31 am

    I am having some kind of error

    firstly in my site i am having php5.0 version, so i tried to use php5.0 (alexia rating) codes given above
    but i get error on this simplexml_load_file function, saying undefined!

    anyways so then i used php4.0 codes, it worked fine, but giving error again at line 31: Warning: preg_match() [function.preg-match]: Unknown modifier ‘]’

    i just copied these codes, what might be the reason?
    Please be a little descriptive, since i have a little knowledge on this object-oriented thing in php.

  6. March 30, 2008 @ 10:34 am

    oh i forgot to say

    the line 31 is this line

    $this->values['rank'] = (preg_match(‘/POPULARITY URL=”([a-z-0-9-./]{1,})” TEXT=”([0-9]{1,12})”/’,$this->xml,$regs) ? number_format($regs[1]) : 0);

    which is having the error

  7. March 30, 2008 @ 10:43 am

    sorry the above line was modified(by me only), but still

    original line 31:

    $this->values['rank'] = (preg_match(‘/POPULARITY URL=”[a-z0-9-./]{1,}” TEXT=”([0-9]{1,12})”/’,$this->xml,$regs) ? number_format($regs[1]) : 0);

    didn’t work :(

  8. March 30, 2008 @ 1:29 pm

    Thank you for the heads up Parijat. WordPress mangled my code. I’ve completely replaced the previous PHP4 “set” function above so use that. You’ll notice many more forward slashes.

  9. April 1, 2008 @ 1:15 am

    hi david

    thx for solving that thing. its working fine now and so i can use it with my top-site.

    Thank you

    For others who r using it:

    One small Tip:

    if you need to make the thing dynamic, replace the constructor.

    Replace:

    $alexa_connector = new alexa(‘http://alexa.com/xml/dad?url=’,'digg.com’); // domain only!

    with this two line

    $site = $_GET['site'];
    $alexa_connector = new alexa(‘http://alexa.com/xml/dad?url=’,$site); // domain only!

    you pass the domain name as get parameter.

    Alternatively, you can use POST method too.

    Hope that helps you.

  10. kevin
    July 5, 2008 @ 3:58 pm

    Hey David,

    How easy would it be to deposit the data into a MySQL database?

    Thanks for a cool tool!

  11. July 5, 2008 @ 5:45 pm

    @Kevin: Extremely easy — you’d just add the MySQL code to my code above.

  12. July 28, 2008 @ 1:22 am

    Yeah.but how can I parse the tag and get the text?

  13. July 28, 2008 @ 7:54 am

    @neekey: It’s all above…

  14. July 28, 2008 @ 8:05 pm

    I mean I want to get more tags like ADDR,LANG,SPEED, and so on.
    could you do it?Thank you very much.
    It is really a useful lib :)

  15. August 15, 2008 @ 3:25 pm

    Absolutely brilliant! Thank you for this class – it’s a real help. :-)

  16. dave
    October 2, 2008 @ 4:46 am

    Thank you for the brilliant class you’ve made. I just want to suggest if you can make it by URL/page not by domain only. Thank you :-)

  17. December 7, 2008 @ 4:55 am

    It seems as though Alexa have changed their method and no longer provide this data for free, but rather by payment through Amazon servers. Is that correct??

  18. kevin
    December 9, 2008 @ 8:38 pm

    Eytan, you might be right: http://aws.amazon.com/awis/

    Question is, how is my Firefox SearchStatus Addon still working?

  19. jason
    January 18, 2009 @ 10:37 pm

    Is CURL a better option? What are the pros/cons? Do you have an opinion?

  20. February 14, 2009 @ 2:25 am

    This is a great post, fortunately 70% of the time of website owner is consumed in experimenting towards increasing page rank and alexa rank. To start with increasing page rank one must start with downloading Alexa toolbar for IE or firefox on as many system possible. as a second step alexa widget on every page of your website, by this way every webpage opened is counted while calculating rank inspite is user does not have alexa toolbar installed knowtheworldaround.com

  21. June 6, 2009 @ 1:52 pm

    The code below cannot fetch the data from alexa –
    $alexa_connector = new alexa(‘http://alexa.com/xml/dad?url=’

    I used PHP5 version and replaced the above code with it. Will try to modify the code and use it on http://seoadsense.free2step.com/index.php/seo-tools/page-rank-checker/ and see how it goes.

  22. ahmad
    June 12, 2009 @ 10:52 am

    it’s not working anymore :(

    anyone got a new code for it ?

  23. June 13, 2009 @ 5:03 pm

    Do you know information about names of field in XML data that Alexa send? Here it is a part of XML data

  24. June 13, 2009 @ 5:04 pm

    Oops.. It looks like your comment engine cut XML data.. Try again

    <ALEXA VER=”0.9″ URL=”google.com/” HOME=”0″ AID=”=”>
    <RLS PREFIX=”http://” more=”255″>
    <RL HREF=”www.zoneedit.com/” TITLE=”ZoneEdit”/>
    <RL HREF=”www.tzo.com/” TITLE=”Tzo.com The Reliable Dynamic Dns Service”/>
    </RLS>
    <SD TITLE=”A” FLAGS=”DMOZ”>

  25. August 23, 2009 @ 1:36 am

    i think the script its not working anymore, because alexa.com use pay subscription for this kind of service

  26. October 2, 2009 @ 6:00 am

    Script not working
    return Warning: file_get_contents(): HTTP request failed! HTTP/1.0 404 Not Found

  27. February 12, 2010 @ 1:52 pm

    ok, if they want $$$ for xml… we can just download the whole page containing the information ;).. but there must be some other way, because there are free toolbars which showing the alexa numbers without parsingthe whole page…

Be Heard!

Share your thoughts with fellow developers of all skill levels! I want to hear from you!

Name*:
Email*:
Website:  
Wrap your code with <code> tags, f00!