PHP using the Alexa API to get the site Alexa ranking Example _php instance

Source: Internet
Author: User
Tags xml parser

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

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.