We can use Alexa's website (or other webmaster Tool website) to search for our website traffic rankings so that 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:
Copy codeThe Code is as follows:
<Alexa ver = "0.9" URL = "jb51.net/" HOME = "0" AID = "ScELh1AI3f00az" IDN = "jb51.net/">
<Rls prefix = "http: //" more = "0"> </RLS>
<Sd title = "A" FLAGS = "" HOST = "jb51.net">
<Linksin num = "1"/>
</SD>
<SD>
<Popularity url = "jb51.net/" TEXT = "7552101" SOURCE = "panel"/>
<Reach rank = "6342897"/>
</SD>
</ALEXA>
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:
Copy codeThe Code is as follows:
<Php>
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 <count ($ vals); $ I ++ ){
If ($ vals [$ I] ["tag"] = "POPULARITY "){
Return $ vals [$ I] ["attributes"] ["TEXT"];
}
}
}
}
?>
Usage:
Copy codeThe Code is as follows:
<? Php
Echo getAlexaRank ("jb51.net ");
?>