[英]PHP simplexml results in Notice: Trying to get property of non-object in error from curl
I must be doing something obviously wrong here: 我肯定在这里做错了明显的事情:
I'm using Curl to retrieve data from beyond a logon saved as $result. 我正在使用Curl从保存为$ result的登录中检索数据。 The XML looks like:
XML看起来像:
<data>
<option>...</option>
<option>...</option>
<option>...</option>
<items>
<item title="label">
<detail_1="abc">
<detail_2="def">
</item>
<item title="label2">
<detail_1="def">
<detail_2="ghi">
</item>
</items>
</data>
I need all the data from each 'item' group only. 我只需要每个“项目”组中的所有数据。
My code: 我的代码:
$xml = simplexml_load_string($result);
foreach($xml->data->items->item as $item)
{
echo $item["label"].'<br>';
}
The result is 结果是
Notice: Trying to get property of non-object in ....
I have not used this function before. 我以前没有使用过此功能。 Please advise.
请指教。
Instead of this echo $item["label"].'<br>';
代替此
echo $item["label"].'<br>';
try this one 试试这个
echo $item['title'].'<br>';
You do like this since the one you want to print is an attribute of the item tag 您之所以喜欢这样,是因为您要打印的是商品标签的属性
foreach($xml->data->items->item->attributes() as $key=>$val)
{
echo $val.'<br>';
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.