All TalkersCode Topics

Follow TalkersCode On Social Media

Find Alexa Rank Of Any Website Using PHP

Last Updated : Apr 15, 2022

IN - PHP HTML

In this tutorial we will show you how to find alexa rank of website using PHP.You can use this as a tool or additional feature in your website and helps user to find alexa rank of any website right on your website.

You may also like create rss feed using PHP.

Find Alexa Rank Of Any Website Using PHP

To Find Alexa Rank It Takes Only Two Steps:-

  1. Make a PHP file and define markup and scripting
  2. Make a CSS file and define styling

Step 1. Make a PHP file and define markup and scripting

We make a PHP file and save it with a name rank.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);
}

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>
 <link href="rank.css"  rel="stylesheet" type="text/css"/>
</head>
<body>
<div id="wrapper">

 <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 class="rank_para"><?php echo $globalRank; ?></p>
 <p class="rank_para"><?php echo $countryRank;?></p>

</div>
</body>
</html>

In this step we create a function to get alexa rank this function is pre defined by alexa to fetch rank you have to only pass the website name.

Then we create a form which is used to enter alexa rank. Then after submiting the form $alexa['globalRank'][0] is used to get the global rank and $alexa['CountryRank']['@attributes']['RANK'] is used to get rank in your own country.

You may also like create sitemap using PHP.

Step 2. Make a CSS file and define styling

We make a CSS file and save it with a name rank.css

body 
{
 text-align:center;
 width:100%;
 margin:0 auto;
 padding:0px;
 font-family:helvetica;
 background-color:#424242;
}
#wrapper
{
 text-align:center;
 margin:0 auto;
 padding:0px;
 width:995px;
}
#rank_form p
{
 color:white;
 font-size:16px;
 font-weight:bold;
}
#rank_form input[type="text"]
{
 width:250px;
 height:40px;
 border:none;
 padding-left:10px;
 font-size:16px;
}
#rank_form input[type="submit"]
{
 background-color:#2E2E2E;
 height:42px;
 border:none;	
 color:white;
 width:50px;
 font-size:16px;
}
.rank_para
{
 color:white;
}

Thats all, this is how to find alexa rank of any website using PHP. You can customize this code further as per your requirement. And please feel free to give comments on this tutorial.

Latest Tutorials