Today's Question:  What does your personal desk look like?        GIVE A SHOUT

Php Class to Retrieve Alexa Rank

  php-html.net        2011-10-25 11:28:55       3,757        0    

Alexa is a service acquired by Amazon which offers a web traffic report for websites. They retrieve the data from toolbar that can be installed in different browsers, centralize the data and display reports to anyone. The most important indicator is the Alexa Rank. It represents the rank of a webpage in a list of all the websites. It’s not the 100% accurate but it gives a good indication.

Alexa does not offer any free API to obtain Alexa Rank. However there is a simple method to obtain it in the same way the Alexa Toolbar does. All you have to do is to invoke the following url(replacing php-html.net with your domain): http://data.alexa.com/data?cli=10&dat=s&url=php-html.net

The response should look like this one:

  1. <alexa ver="0.9" url="php-html.net/" home="0" aid="=">  
  2. <sd title="A" flags="" host="php-html.net">  
  3. <linksin num="20">  
  4. </linksin></sd>  
  5. <sd>  
  6. <popularity url="php-html.net/" text="785463">  
  7. <reach rank="768874">  
  8. <rank delta="-941784">  
  9. </rank></reach></popularity></sd>  
  10. </alexa>  

All we have to do is to take the response and extract the Alexa Rank from it. There are various options, we are just going to use a simple regular expression:

  1. class AlexaRank  
  2. {  
  3.     public function getRequestUri($domain)  
  4.     {  
  5.         return 'http://data.alexa.com/data?cli=10&dat=s&url=' . $domain;  
  6.     }  
  7.   
  8.     public function parse( $httpResponse )  
  9.     {  
  10.         preg_match( '#<popularity url="(.*?)" text="([0-9]+){1,}">#si'$httpResponse$p );  
  11.         $res = ( $p[2] ) ? number_format( intval($p[2]) ):0;  
  12.   
  13.         return str_replace(','''$res);  
  14.     }  
  15. }  
  16. </popularity>  

Then we can simply get the alexa rank:

  1. echo $extractor->parse(file_get_contents($extractor->getRequestUri('php-html.net')));  

The extractor class contains the 2 functions one to build the url to be invoked for a domain and the second one to parse the response. This solution was chosen offer the option to use an external function that retrieve the response content. This is because for some hosting servers the file_get_contents does not work, for other curl does not work…

Remember the “use it but not abuse it” concept. Alexa didn’t expose this through a public api for some reason. If you use this function zillions times a day you will probably be detected and have the ip banned

P.S From Editor: I think we can also use DOMDocument object in PHP to retrieve the RANK from the retrieved rank XML from Alexa.Here is the demo code:

$extractor=new AlexaRank();

$doc=new DOMDocument();

$doc->loadXML(file_get_contents($extractor->getRequestUri('pixelstech.net'))); 

$nodeLisr=$doc->getElementsByTagName('POPULARITY')->item(0);

echo $nodeLisr->getAttribute('TEXT');

Source:http://php-html.net/tutorials/php-class-to-retrieve-alexa-rank/

PHP  API  ALEXA  ALEXA RANK  RETRIEVE ALEXA RANK 

Share on Facebook  Share on Twitter  Share on Weibo  Share on Reddit 

  RELATED


  0 COMMENT


No comment for this article.