如何从 alexa API 获取竞争对手记录到数组
Posted
技术标签:
【中文标题】如何从 alexa API 获取竞争对手记录到数组【英文标题】:How to fetch competitors record from alexa API to array 【发布时间】:2016-12-26 05:24:27 【问题描述】:我打电话给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 属性。
【问题讨论】:
【参考方案1】:这将打印出所有的 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'];
【讨论】:
【参考方案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 />';
【讨论】:
【参考方案3】:这是另一种解决方案。您不必像 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/>';
?>
【讨论】:
【参考方案4】:使用下面的代码。
<?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;
?>
【讨论】:
以上是关于如何从 alexa API 获取竞争对手记录到数组的主要内容,如果未能解决你的问题,请参考以下文章