Alexa Ranking Checker

This sections will contains code regarding different PHP & MySQL features and contains help about php/mysql issues.

Alexa Ranking Checker

Postby MT Shahzad on March 17th, 2008, 11:41 am

Here is the php code to check the ranking of a website on the Alexa.com

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";
    }
?>
MT Shahzad
Web/Software Developer
http://mts.sw3solutions.com
User avatar
MT Shahzad
Site Admin
 
Posts: 300
Joined: February 29th, 2008, 8:11 am
Location: Muridke, Pakistan

Return to PHP / MySQL / XML

Who is online

Users browsing this forum: No registered users and 0 guests

cron