简体   繁体   中英

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. The XML looks like:

<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>'; 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>';
  }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM