PHP Code to Get Alexa Rank

By | September 2, 2012

Every website have an unique alexa rank. To see the alexa rank of our site, we have to go to the www.alexa.com or install alexa plugin or place alexa widget in our website. By using php code also we can get alexa rank for our website. Here i have used php code to get alexa rank. In this http://data.alexa.com/data?cli=10&dat=snbamz&url=URL , alexa rank and its associated details for the provided website will be displayed. preg_match() php function will be used to get the alexa rank from the page source.

Using preg_match() php function, <popularity> will be find of  from the page and stored in $alexa array variable.

$alexa[1] – This will result URL, which we provided.

$alexa[2] – This array varialble will store alexa rank.

[code type=php]

<html>
<form action=”” method=”post”>
Enter Your Website: <input type=”text” name=”url” />
<input type=”submit” name=”submit” />
</form>
<?php
if(isset($_POST[‘submit’]))
{
$url=$_POST[‘url’];
$url1 = file_get_contents(‘http://data.alexa.com/data?cli=10&dat=snbamz&url=’.$url);
preg_match(‘/<popularity url=”(.*?)” text=”([0-9]+)” source=”panel”\/>/si’, $url1, $alexa);
$rank = ($alexa[2]) ? $alexa[2] : 0;
echo $rank;
}
?>
</html>

[/code]

2 thoughts on “PHP Code to Get Alexa Rank

Leave a Reply to Aaryadev Cancel reply

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