Sample Code: Determine Alexa Ranking using cURL

Posted on Posted in Blog

PHP has a set of functions for libcurl – a library of functions to access a URL (Uniform Resource Locator). These functions could be used to enable your script to talk with other servers in the internet. Data could be exchanged between a remote server and your script and it could be done in both directions. PHP’s cURL supports different protocols like HTTP, FTP, GOPHER and others commonly used in the internet.

The PHP cURL functions are useful in a lot of ways. A script can download information from a server or post information to that server without human intervention. And not just that, logging in to secure sites (HTTPS) and submit or fetch data is doable also. In fact, the cURL set of functions is the most popular tool used by web scrapers (scripts fetching data from servers and saving these data on the local server/database).

Let me show you how cURL could be used to determine a website’s Alexa Ranking. And its up to you to gauge how powerful these functions are in fetching information from the internet.

function fetch_alexa_ranking ($site){
$url = “{$site}&r=home_home&p=bigtop”;

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

$content = curl_exec ($ch);
$content = str_replace (array(“rn”, “n”, “t”), “”, $content);
preg_match_all (‘/Alexa Traffic Rank:(.*?)/’, $content, $result, PREG_SET_ORDER);

$rank = str_replace (“,”, “”, $result[0][2]);
return $rank;

Although there are different methods of accessing data offered by Alexa like using the AWIS (Alexa Web Information Service) which is a commercial service, I’m showing you the above script just to prove a point.

You see, we’re only getting data from a service they are offering as free – from their website. And that is easily done with PHP cURL.

The following curl functions are used: curl_init – used to initialize the cURL library; curl_setopt – used to set variables and operations in the library; and curl_exec – execute the cURL. Another very important PHP function used is preg_match_all which is used to extract the information from the gathered html.

Basically, what the function fetch_alexa_ranking does is go to the Alexa website and fetch a webpage from the search script. It also feed the site to retrieve information.

To call the function write something like this in your script:

$website = “”
echo “The Alexa Ranking for the website {$website} is: ” . fetch_alexa_ranking ($website);

There you go, I hope you learn something from this. Sometimes, there are websites that doesn’t accept posts from automated scripts. There is a simple way to solve this and I want you to check this article: PHP Script Pretending to be a Browser