in PHP

Get Any Website Alexa Rank Using PHP

In past tutorials here you have learned how to get your Twitter followers and your Feedburner subscriber numbers. They are both useful information to use to judge how popular your website is. But there is another way to judge how popular your website is and that is your Alexa Ranking. Alexa the Web Information Company

What is Alexa Ranking?

Alexa is the leading provider of free, global web metrics. Millions of people from all over the world visit Alexa to get the data they need to make smart business decisions. Search Alexa to discover the most successful sites on the web by keyword, category, or country. Alexa requires it's users to download their toolbar so they can keep track of web analytic data. Therefore Alexa will track which websites you are going on so it can create a database with this data. From this data Alexa ranks all the websites in the world. The lower your Alexa Ranking is the more popular your website is. According to Alexa here are the Top 10 most popular websites on the internet. - Google

  • Facebook
  • YouTube
  • Yahoo!
  • Wikipedia
  • Windows Live
  • Twitter
  • QQ.COM

    As you can see from this list the results are as you would expect, Google being the most popular website and then Facebook, this is why Alexa is a good judge to see how popular your website is. Obviously it's not accurate enough to be a full judge because in order to track how popular a website is the visitor will need to have downloaded the Alexa toolbar. For this reason you will most likely see technology niche websites being higher in the Alexa ranking. ## Get Your Website Alexa Rank

    In this tutorial you will learn how to get your website Alexa ranking so you can display it anyway you want. We will be using PHP and cURL to get your Alexa ranking. Demo First were start off by creating a new PHP class Get_Alexa_ranking. This can be used to get other information from the Alexa API by for this example we are just going to be getting the rank of a domain.

<?php /**

  • PHP Class to get a website Alexa Ranking
  • @author
  • */ class Get_Alexa_Ranking{


    • Get the rank from alexa for the given domain
    • @param $domain
    • The domain to search on */ public function get_rank($domain){

      $url = "".$domain;

      //Initialize the Curl
      $ch = curl_init();

      //Set curl to return the data instead of printing it to the browser.
      curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch,CURLOPT_CONNECTTIMEOUT,2);

      //Set the URL
      curl_setopt($ch, CURLOPT_URL, $url);

      //Execute the fetch
      $data = curl_exec($ch);

      //Close the connection

      $xml = new SimpleXMLElement($data);

          //Get popularity node

      $popularity = $xml->xpath("//POPULARITY");

          //Get the Rank attribute

      $rank = (string)$popularity[0]['TEXT'];

      return $rank; }

} ?>

 Above is the PHP class we are going to use. There is only 1 function in the class which requires a parameter which is the domain we are searching on. We use cURL to get the data from the URL, we can then turn this data into an XML object, now it is easy to get the data inside the object. We use xpath to the popularity node, once we have this node we can get the text attribute which is your Alexa rank number. Now we have this class we can use it to get our the Alexa ranking of   

//Include alexa ranking class require_once 'get_alexa_ranking.php';

//Create a new object $alexa = new Get_Alexa_Ranking();

//Get the rank for the domain echo "Rank ".$alexa->get_rank("");

 View the demo to see Alexa Rank. [Demo](

Upgrade to access all content on Paulund

Members unlock all tutorials and snippets

Access to all downloadable content

Access to code examples before others

Sign Up Now

Already a member? Login here

Subscribe To The Weekly Newsletter

Get weekly updates to your email