Today one of my internet friend share with us a php code to fetch alexa ranking.
- All you need to do copy the code from here.
<?php
$Url = $_GET[‘url’];
function getPage ($url) {
if (function_exists(‘curl_init’)) {
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_USERAGENT, $_SERVER[‘HTTP_USER_AGENT’]);
return curl_exec($ch);
} else {
return file_get_contents($url);
}
}
function getAlexaRank($url) {
$url = @parse_url($url);
$url = $url[‘host’];
$url = “http://data.alexa.com/data?cli=10&dat=s&url=$url”;
$data = getPage($url);
preg_match(‘#<POPULARITY URL=”(.*?)” TEXT=”([0-9]+){1,}”/>#si’, $data, $p);
$value = ($p[2]) ? number_format($p[2]) : 0;
return $value;
}
$Rank = getAlexaRank($Url);
echo $Rank;
?> - Save the file in your site http://www.yourdomain.com/alexapagerank.php
- Now run the file from browser http://www.yourdomain.com/alexapagerank.php?url=domain_name_to_get_alexa_ranking
Easy and simple step to get any of your website alexa ranking from your site.
Thanks to share this valuable code -NB-
8 comments
Comments feed for this article
April 28, 2007 at 11:21 am
susenjit
good code. It’s helpful for them who likes to show the alexa ranking in their website or blog. This code can be modified to display the alexa ranking for the same site or blog. I think this can be good plugin too.
July 26, 2007 at 4:11 am
Mr_Bill
Interesting comment since the code gives a error
Parse error: syntax error, unexpected ‘:’ on line 17
April 4, 2008 at 10:51 am
Asylum
I have revised the code a bit to make it a tiny bit faster although you should probably cache the results so you do not get banned by alexa for to many queries against them. Also Mr Bill, the code worked just fine except for a few single and double quotes that had been converted by whatever editor was being used. Either that or it was intentionally done to stop copy pasta.
<?php
function getPage($url) {
if (function_exists(‘curl_init’)) {
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_USERAGENT, $_SERVER[‘HTTP_USER_AGENT’]);
return curl_exec($ch);
} else {
return file_get_contents($url);
}
}
function getAlexaRank($url) {
$url = @parse_url($url);
preg_match(‘##si’, getPage(‘http://data.alexa.com/data?cli=10&dat=s&url=’.$url[‘host’]), $p);
return (empty($p[2])) ? 0 : number_format($p[2]);
}
echo getAlexaRank($_GET[‘url’]);
?>
April 4, 2008 at 10:55 am
Asylum
Grr, code was stripped from the preg_match and should be like this.
preg_match(‘#<POPULARITY URL=”(.*?)” TEXT=”([0-9]+){1,}”/>#si’, getPage(‘http://data.alexa.com/data?cli=10&dat=s&url=’.$url[‘host’]), $p);
March 16, 2010 at 8:46 pm
desis
Thanks for the code
April 23, 2010 at 6:42 pm
yreadthis
alexa has become one of the great site which displays the traffic level of other sites and hope this code should help the readers…
August 31, 2010 at 6:04 am
baljit
Awsommmme..thanks..i was looking for this one..it works..the original post is working well..
August 31, 2010 at 6:16 am
baljit
awesommmme…gr8..i was looking for this…original code is working..no changes in that..
Thanks a lot..