PHP uses the Alexa API to obtain the website's Alexa ranking example

Source: Internet
Author: User
Tags xml parser

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

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.