by MT Shahzad on March 21st, 2008, 8:26 am
The following php code allows you to check the Alexa Ranking of a website.
- Code: Select all
<?
@ini_set("max_execution_time", 0);
if (!$_GET)
{
?>
<html>
<body>
<h3>Alexa Ranking</h3>
<form method="get" action="alexa.php">
<input type="text" name="url" size="40" value="http://www.google.com/">
<input type="submit" value="Find">
</form>
<?
}
else
{
$url = $_GET['url'];
$url = str_replace("http://", "", $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 toInt ($string) {
return preg_replace('#[^0-9]#si', '', $string);
}
$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(toInt($p[2])) : 0;
echo "Alexa Rank: $value";
}
?>