Fetch alexa rank using php script

We all know alexa is one of the very popular tool to check website popularity by their rank, It’s traffic rank depends on varies factors, Alexa has given a open url to get some of your’s website traffic data like global and country specific rank, Alexa also provide a paid API to fetch deep analysis of any websites traffic. If you want to improve your website alexa rank read this article of mine: How to improve/increase alexa ranking & why it’s important



Here i created a simple function in php to fetch some basic information of website like their global and country specific rank, You can customize below function as per your need. Alexa has provided public url http://data.alexa.com/data?cli=10&url=[WEBSITE_NAME] to get information about website rank.
alexa-rank-php

Function to fetch website’s alexa data

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);
}

Pass any website url on this function and it’ll return their alexa rank. If you want to fetch more information pass one more tag on alexa public url: http://data.alexa.com/data?cli=10&dat=snbamz&url=[WEBSITE_NAME]


Complete code.

index.php

<?php
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);
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
  <title>Alexa Rank checker tool in php</title>
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
  <style>
  body {
    font-family: Helvetica,Arial,sans-serif;
    font-size: 14px;
    line-height: 22px;
    text-align: center;
}
  </style>
</head>
<body>
<div>
<h1>Alexa Rank checker tool in php</h1>
<p> Simple php based script to fetch website's alexa rank. </p>
<form method="get" action="">
<div>
		<p>Enter your domain name.</p>
		<p><input name="siteinfo" style="width:350px;" placeholder="E.g. www.iamrohit.in" required="required"/></p>
 
 
		<p><input type="submit" value="Get Alexa Rank" id="qr-gn"></p>
</div>
</form>
<br/>
<?php
if(isset($_REQUEST['siteinfo'])) {
 	$url = $_REQUEST['siteinfo'];
    $alexa = alexaRank($url);
    $globalRank = isset($alexa['globalRank'][0]) ? $alexa['globalRank'][0] : 'N/A';
    $countryRank = isset($alexa['CountryRank']['@attributes']['RANK']) ? $alexa['CountryRank']['@attributes']['RANK'] : 'N/A';
    echo "<h1>".$url." global alexa rank is <b style='color:red'>".$globalRank."</b> And Country (".$alexa['CountryRank']['@attributes']['NAME'].") rank is <b style='color:red'>".$countryRank."</b></h3>";
 }
 ?>
<div></div>
</body>
</html>

See live demo and download source code.

DEMO

DOWNLOAD

If you like this post please don’t forget to subscribe my public notebook for more useful stuff