我很难过。我正在尝试连接到一个返回XML文件的简单API。问题是,当我在浏览器中访问URL时,一切看起来都不错,但是当我在PHP中调用该URL时,它会复制对象两次。因此,所有值都是重复的,当URL只有1时,有2个对象。
我的PHP文件中包含的内容(我有更多内容,但我尽可能保持原始状态以确保消除任何用户错误问题):
print_r( simplexml_load_file('https://api.uptimerobot.com/getMonitors?apiKey=API_KEY_NUMBER') );
我在浏览器中加载网址时收到的内容(为了简单和隐私而移除了无关的数据)
<monitors offset="0" limit="50" total="96">
<monitor id="ID" friendlyname="FRIENDLYNAME" url="URL"... />
<monitor id="ID" friendlyname="FRIENDLYNAME" url="URL"... />
<monitor id="ID" friendlyname="FRIENDLYNAME" url="URL"... />
<monitor id="ID" friendlyname="FRIENDLYNAME" url="URL"... />
<monitor id="ID" friendlyname="FRIENDLYNAME" url="URL"... />
<monitor id="ID" friendlyname="FRIENDLYNAME" url="URL"... />
<monitor id="ID" friendlyname="FRIENDLYNAME" url="URL"... />
</monitors>
当我从我的网站print_r()时获得的内容(为了隐私和简单而简化)
SimpleXMLElement Object ( [@attributes] => Array ( [offset] => 0 [limit] => 50 [total] => 96 ) [monitor] => Array (
[0] => SimpleXMLElement Object ( [@attributes] => Array ( [id] => ID [friendlyname] => FRIENDLYNAME [url] => URL ...
[1] => SimpleXMLElement Object ( [@attributes] => Array ( [id] => ID [friendlyname] => FRIENDLYNAME [url] => URL ...
[2] => SimpleXMLElement Object ( [@attributes] => Array ( [id] => ID [friendlyname] => FRIENDLYNAME [url] => URL ...
[3] => SimpleXMLElement Object ( [@attributes] => Array ( [id] => ID [friendlyname] => FRIENDLYNAME [url] => URL ...
[4] => SimpleXMLElement Object ( [@attributes] => Array ( [id] => ID [friendlyname] => FRIENDLYNAME [url] => URL ...
[5] => SimpleXMLElement Object ( [@attributes] => Array ( [id] => ID [friendlyname] => FRIENDLYNAME [url] => URL ...
[6] => SimpleXMLElement Object ( [@attributes] => Array ( [id] => ID [friendlyname] => FRIENDLYNAME [url] => URL ...
) ) )
SimpleXMLElement Object ( [@attributes] => Array ( [offset] => 0 [limit] => 50 [total] => 96 ) [monitor] => Array (
[0] => SimpleXMLElement Object ( [@attributes] => Array ( [id] => ID [friendlyname] => FRIENDLYNAME [url] => URL ...
[1] => SimpleXMLElement Object ( [@attributes] => Array ( [id] => ID [friendlyname] => FRIENDLYNAME [url] => URL ...
[2] => SimpleXMLElement Object ( [@attributes] => Array ( [id] => ID [friendlyname] => FRIENDLYNAME [url] => URL ...
[3] => SimpleXMLElement Object ( [@attributes] => Array ( [id] => ID [friendlyname] => FRIENDLYNAME [url] => URL ...
[4] => SimpleXMLElement Object ( [@attributes] => Array ( [id] => ID [friendlyname] => FRIENDLYNAME [url] => URL ...
[6] => SimpleXMLElement Object ( [@attributes] => Array ( [id] => ID [friendlyname] => FRIENDLYNAME [url] => URL ...
) ) )
我的想法已经用完了!当我在下面尝试做的时候我第一次遇到这个问题并且它返回了两次值。
$xml = simplexml_load_file('https://api.uptimerobot.com/getMonitors?apiKey=API_KEY');
$total_sites = $xml['total'];
echo $total_sites;
回声会返回9696
答案 0 :(得分:0)
经过进一步审查,我实际上是两次添加它。我删除了其中一个参考文献,它就像一个魅力!