我致电http://data.alexa.com/data?cli=10&dat=snbamz&url=www.flipkart.com获取网站数据,当我在浏览器上运行时,它会显示此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>
我正在以此获取人气和国家排名。
$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;
它工作正常,但我不知道如何获取数组中所有RL标记 HREF 属性。
答案 0 :(得分:4)
这将打印出所有href值。我首先将xml转换为json,然后循环遍历它。
$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'];
}
答案 1 :(得分:2)
我希望它会对你有所帮助。
$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 />';
}
答案 2 :(得分:2)
这是另一种解决方案。您不必像xDiglett解释的那样使用json_decode()
和json_encode()
,而是直接访问XML属性。
$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/>';
}
?>
答案 3 :(得分:0)
使用以下代码。
<?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;
?>