13

I call http://data.alexa.com/data?cli=10&dat=snbamz&url=www.flipkart.com to get website data and when I run on browser it's display this XML.

<ALEXA VER="0.9" URL="flipkart.com/" HOME="0" AID="=" IDN="flipkart.com/">
<RLS PREFIX="http://" more="0">
<RL HREF="storewala.com/" TITLE="Storewala.com - Indian Retail Store"/>
<RL HREF="pdf-search-engine.com/" TITLE="Ebook Search - Pdf Search Engine"/>
<RL HREF="overnitenet.com/" TITLE="-----Overnite-----"/>
<RL HREF="nbcindia.com/" TITLE="Online Book Stores, India's Largest Bookstore, Nbc India.com"/>
<RL HREF="mouthshut.com/" TITLE="Mouthshut.com - Consumer Reviews Helping You Decide"/>
<RL HREF="landmarkonthenet.com/" TITLE="Land Mark"/>
<RL HREF="ingrambook.com/" TITLE="Ingram Book Group"/>
<RL HREF="infibeam.com/" TITLE="InfiBeam - Gifts to India | Online Shopping India | Books, Cameras, Watches, Mob"/>
<RL HREF="indiaplaza.in/" TITLE="Fabmall House"/>
<RL HREF="firstflight.net/" TITLE="FirstFlight Courier"/>
</RLS>
<SD TITLE="A" FLAGS="" HOST="flipkart.com">
<TITLE TEXT="Flipkart.com"/>
<OWNER NAME="Flipkart.com"/>
</SD>
<SD>
<POPULARITY URL="flipkart.com/" TEXT="116" SOURCE="panel"/>
<REACH RANK="130"/>
<RANK DELTA="+5"/>
<COUNTRY CODE="IN" NAME="India" RANK="8"/>
</SD>
</ALEXA>

and I am fetching popularity and country rank with this.

$xml = simplexml_load_file('http://data.alexa.com/data?cli=10&dat=snbamz&url=www.flipkart.com');
$rank=isset($xml->SD[1]->POPULARITY)?$xml->SD[1]->POPULARITY->attributes()->TEXT:0;
$country_rank=isset($xml->SD[1]->COUNTRY)?$xml->SD[1]->COUNTRY->attributes()->RANK:0;

and it's working fine but I don't have any idea how to fetch all RL tags HREF attributes in array.

Aju
  • 503
  • 1
  • 8
  • 26
Divyesh Jesadiya
  • 1,105
  • 4
  • 30
  • 68

4 Answers4

5

This will print out all href values. I first convert the xml to json, then loop through it.

$url = 'http://data.alexa.com/data?cli=10&dat=snbamz&url=www.flipkart.com';
$xml = simplexml_load_string(file_get_contents($url));
$json = json_decode(json_encode($xml),TRUE);

foreach ($json['RLS']['RL'] as $url) {
    echo $url['@attributes']['HREF'];
}
reinierkors
  • 507
  • 6
  • 19
3

I hope it will help you.

$xml = simplexml_load_file('http://data.alexa.com/data?cli=10&dat=snbamz&url=www.flipkart.com');

foreach($xml->RLS->children() as $key => $val){

    $href = (string)($val->attributes()->HREF);
    $title= (string)($val->attributes()->TITLE);

    echo 'HREF : ' .  $href . ' TITLE: ' . $title . '<br />';
}
3

Here is another solution. You don't have to use json_decode() and json_encode() as xDiglett explained, but to access the XML attributes directly.

$url = 'http://data.alexa.com/data?cli=10&dat=snbamz&url=www.flipkart.com';

$xml = new SimpleXMLElement(file_get_contents($url))  or die("Error: Cannot create object");

foreach ($xml->RLS->RL as $element) {
    echo $element->attributes()->HREF.' - '.$element->attributes()->TITLE.'<br/>';
}

?>
Tosho Trajanov
  • 790
  • 8
  • 19
0

Use below code.

<?php 
$url='example.com';
$xml = simplexml_load_file('http://data.alexa.com/data?cli=10&dat=snbamz&url='.$url);
$rank=isset($xml->SD[1]->POPULARITY)?$xml->SD[1]->POPULARITY->attributes()->TEXT:0;
$web=(string)$xml->SD[0]->attributes()->HOST;
echo $web." has Alexa Rank ".$rank;
?>
Prem Tiwari
  • 84
  • 1
  • 5