We will use Alexa website (or other webmaster tools site) To check our site traffic rankings, so we have to go to those sites. In fact, the Alexa XML API can be obtained from the Alexa-related data (XML format), and then use the XML parser to parse the Alexa returned XML, get Alexa rankings 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%
The data returned with Http://data.alexa.com/data?cli=10&dat=snbamz&url=jb51.net is as follows:
Copy Code code 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>
Where the value of the Text property in the popularity element is 7552101, the Alexa rank.
Code implementation:
Use PHP to achieve Alexa rankings through the Alexa API code:
Copy Code code 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_into_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"];
}
}
}
}
?>
How to use:
Copy Code code as follows:
<?php
Echo Getalexarank ("jb51.net");
?>