<resources>
<merchants count="7">
<merchant id="1300" name="Wal-M" count="1" logo_url="" url=""/>
<merchant id="1387" name="Nothing.com" count="1" logo_url="" url=""/>
<merchant id="3486" name="Micro" count="1" logo_url="" url=""/>
<merchant id="13880" name="Sea" count="1" logo_url="" url=""/>
<merchant id="13881" name="Kma" count="1" logo_url="" url=""/>
<merchant id="14711" name="Cas Interstate Music" count="1" logo_url="" url=""/>
<merchant id="1882969" name="Targ" count="1" logo_url="" url="http://r.url.com"/>
</merchants>
我有这个xml文件,它返回商家的每个API调用的随机数我始终需要来自商家的url
属性,名称为Targ
我的代码:
$fetchurl = "API URL";
$xml = simplexml_load_file($fetchurl);
$merchantInfo=$xml->resources->merchant; <---stuck here how to get the url of Targ?
答案 0 :(得分:0)
首先,您的xml无效。它错过了根关闭</resources>
。
其次,你可以使用SimpleXMLElement
并使用xpath和target Targ
:
$data = '<resources><merchants count="7"><merchant id="1300" name="Wal-M" count="1" logo_url="" url=""/><merchant id="1387" name="Nothing.com" count="1" logo_url="" url=""/><merchant id="3486" name="Micro" count="1" logo_url="" url=""/><merchant id="13880" name="Sea" count="1" logo_url="" url=""/><merchant id="13881" name="Kma" count="1" logo_url="" url=""/><merchant id="14711" name="Cas Interstate Music" count="1" logo_url="" url=""/><merchant id="1882969" name="Targ" count="1" logo_url="" url="http://r.url.com"/></merchants></resources>';
$xml = new SimpleXMLElement($data);
$url = (string) $xml->xpath('//merchant[@name="Targ"]')[0]->attributes()->url;
echo $url; // http://r.url.com