PHP uses AlexaAPI to obtain the website's Alexa ranking example _ php instance

Source: Internet
Author: User
This article describes how to use AlexaAPI to obtain the Alexa ranking of a website in PHP. For more information, see The Alexa website (or other webmaster tool website) to link our website traffic rankings, so we have to go to those websites. In fact, you can use the Alexa xml api to obtain the Alexa-related data (in XML format) of the website, and then use the XML parser to parse the XML returned by Alexa to obtain the Alexa ranking or other data.

Alexa interface

Alexa's xml api interface is: http://data.alexa.com/data? Cli = 10 & url = % YOUR_URL %

If you want to get more data, you can use: http://data.alexa.com/data? Cli = 10 & dat = snbamz & url = % YOUR_URL %

With http://data.alexa.com/data? The data returned by cli = 10 & dat = snbamz & url = jb51.net is as follows:

The code is as follows:












The value of the TEXT attribute in the POPULARITY element is 7552101, which is the Alexa ranking.

Code implementation:

The code for retrieving the Alexa ranking using the Alexa API in PHP is:

The code is as follows:



Function getAlexaRank ($ Domain ){
$ Line = "";
$ Data = "";
$ URL = "http://data.alexa.com/data? Cli = 10 & dat = snba & url = ". $ Domain;
$ Fp = fopen ($ URL, "r ");
If ($ fp ){
While (! Feof ($ fp )){
$ Line = fgets ($ fp );
$ Data. = $ line;
}
$ P = xml_parser_create ();
Xml_parse_pai_struct ($ p, $ data, $ vals );
Xml_parser_free ($ p );
For ($ I = 0; $ I If ($ vals [$ I] ["tag"] = "POPULARITY "){
Return $ vals [$ I] ["attributes"] ["TEXT"];
}
}
}
}
?>


Usage:

The code is as follows:


Echo getAlexaRank ("jb51.net ");
?>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.