0

My simpleXMLElement output

SimpleXMLElement Object
(
[guid] => 2898580
[title] => England's doomed inevitability? 
[description] => 
[link] => http://www.espnfc.com/blog/the-match/60/post/2895340/roy-hodgson-rings-the-changes-but-england-struggle-again
[pubDate] => Jun 20, 2016 04:58 PM PDT
[enclosure] => SimpleXMLElement Object
    (
        [@attributes] => Array
            (
                [length] => 150
                [url] => http://a.espncdn.com/combiner/i/?img=/photo/2016/0620/r94714_1296x729_16-9.jpg&w=100&h=80&scale=crop&site=espnfc
                [type] => image/jpeg
            )

    )

[category] => null
) 

PHP with help of foreach loop when i print in it

 echo "Img_Url:".$img_url = $item->enclosure->attributes['url'] ;  

it will show blank in there but i write this

echo "Img_Url:".$img_url = $item->enclosure->@attributes['url'] ; 

it gives syntax error .

so how can i print this .

jophab
  • 5,356
  • 14
  • 41
  • 60
gauravK
  • 197
  • 1
  • 2
  • 12

1 Answers1

2

To access the attributes, you just need to treat the SimpleXMLElement object as an array. Don't go digging around in the object's internal structure.

 echo "Img_Url: " . (string) $item->enclosure['url'];

See http://php.net/manual/en/simplexml.examples-basic.php

iainn
  • 16,826
  • 9
  • 33
  • 40