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 ");
?>