How to Find Alexa Rank of any Website using PHP

Alexa traffic rank is a value given to any website by alexa based on it’s traffic. A lower traffic rank means higher traffic volume to a website. Alexa is providing an paid API to access it database. But there is a way to do this for free. To get this data in XML format use following URL. You may also like How to Interval Timer Countdown using setTimeout().

PHP Code

<?php
$globalRank = '';
$countryRank = '';
function alexaRank($url) 
{
    $alexaData = simplexml_load_file("http://data.alexa.com/data?cli=10&url=".$url);
    $alexa['globalRank'] =  isset($alexaData->SD->POPULARITY) ? $alexaData->SD->POPULARITY->attributes()->TEXT : 0 ;
    $alexa['CountryRank'] =  isset($alexaData->SD->COUNTRY) ? $alexaData->SD->COUNTRY->attributes() : 0 ;
    return json_decode(json_encode($alexa), TRUE);
}

if(isset($_GET['siteinfo'])) 
{
    $url = $_GET['siteinfo'];
    $alexa = alexaRank($url);
    $globalRank ="Global Alexa Rank of ".$_GET['siteinfo']." is : ".$alexa['globalRank'][0];
    $countryRank ="Alexa Rank In ".$alexa['CountryRank']['@attributes']['NAME']." is : ".$alexa['CountryRank']['@attributes']['RANK'];
}
?>

<html>
    <head>
    </head>
    <body>
        <form method="get" id="rank_form">
            <p>Enter Website To Get Alexa Rank</p>
            <input type="text" name="siteinfo" placeholder="E.g. www.talkerscode.com" required="required"/>
            <input type="submit" value="Find">
        </form>

        <p><?php echo $globalRank; ?></p>
        <p><?php echo $countryRank; ?></p>
    </body>
</html>

Leave a Reply

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