≡ Menu

PHP script to get Alexa rank for free

The question – Is there a way to get the Alexa rank of a website from a PHP script, through an API, for free?

Alexa rank is a number given by Amazon’s company Alexa which represents the amount of traffic that a site receives. This is calculated using the Alexa toolbar. The lower the traffic rank, the higher the amount of traffic. At this time, Google is rank 2 and Facebook is rank 1 which means that Facebook.com is the most visited site on the web.

How to get the Alexa rank from PHP?

Amazon provides access to the Alexa rank through a service in Amazon web services (AWS) for a small fee. But it is also possible to get the Alexa rank for free using an API that is not very well known.

The URL to get the Alexa rank and some other useful fields in XML format is:

http://data.alexa.com/data?cli=10&dat=s&url=a9.com

The above URL oututs the following XML:

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

<ALEXA VER="0.9" URL="a9.com/" HOME="0" AID="=" IDN="a9.com/">
<SD TITLE="A" FLAGS="DMOZ" HOST="a9.com">
<TITLE TEXT="A9.com"/>
<ADDR STREET="PO Box 504" CITY="Palo Alto" STATE="CA" ZIP="94302" COUNTRY="US" />
<CREATED DATE="1-Mar-2004" DAY=" 1" MONTH="03" YEAR="2004"/>
<PHONE NUMBER="Unlisted"/>
<OWNER NAME="A9.com"/>
<EMAIL ADDR="Unlisted"/>
<DOS>
<DO DOMAIN="opensearch.org" TITLE="opensearch.org"/>
</DOS>
<LANG LEX="en"/>
<LINKSIN NUM="2720"/>
<SPEED TEXT="1101" PCT="69"/>
<REVIEWS AVG="4.5" NUM="17"/>
<CHILD SRATING="0"/>
</SD>

<KEYWORDS>
<KEYWORD VAL="Searching"/>
<KEYWORD VAL="Multi-Search"/>
</KEYWORDS><DMOZ>
<SITE BASE="a9.com/" TITLE="اي ناين" DESC="محرك بحث اي ناين">
<CATS>
<CAT ID="Top/World/Arabic/إقليمـي/الشرق_الأوسط/السعودية/تجارة_و_أقتصاد/كمبيوتر_و_إنترنت/محركات_بحث" TITLE="كمبيوتر و إنترنت/محركات بحث" CID="204954"/>
</CATS>
</SITE>
</DMOZ>
<SD>
<POPULARITY URL="a9.com/" TEXT="8569" SOURCE="panel"/>
<REACH RANK="6486"/>
<RANK DELTA="-2694"/>
<COUNTRY CODE="JP" NAME="Japan" RANK="601"/>
</SD>
</ALEXA>

Note that it has several important fields like popularity, DMOZ listings, the countries in which the site is most popular etc.

To parse this XML, you can use the following PHP code. In the following PHP script, the first few lines get the XML from the alexa.com URL and the functions parse the XML and return the appropriate fields. The function called alexa extracts the Alexa traffic rank. The createddate function extracts the date on which the domain name was created; and the dmozcategory function gets one (of possibly many) DMOZ category from the XML.

<?php
$url = "http://data.alexa.com/data?cli=10&dat=s&url=a9.com";
$xml = simplexml_load_file($url);
$alexaRank = alexa($xml);
$createdDate = createddate($xml);
$DMOZCategory = dmozcategory($xml);

function alexa($xml){
    $result = $xml->xpath("//POPULARITY[@TEXT]");
    $new = $result[0];
    if(!$result){
        $numb = 'N'; 
    }else{ 
        $numb = $new->attributes()->TEXT;
    }
    return $numb;
}

function createddate($xml){
    $result = $xml->xpath("//CREATED[@DATE]");
    $new = $result[0];
    if(!$result){
        $numb = 'Unknown';
    }else{ 
        $numb = $new->attributes()->DATE;
    }
    return $numb;
}

function dmozcategory($xml){
    $result = $xml->xpath("//CAT[@ID]");
    $new = $result[0];
    if(!$result) {
        $numb = 'None';
    }else{
        $numb = $new->attributes()->ID;
    }
    return $numb;
}
?>

The above code extracts the Alexa traffic rank, the created date of the domain name and one DMOZ category. All this for free!

Incoming search terms:

  • bash script get alexa rank (1)

Comments on this entry are closed.