clankill3r Posted November 26, 2011 Share Posted November 26, 2011 I want to get the url in the [@attributes] Atm i get the title like this foreach ($xmlTree->channel->item as $item) { echo '<p>'.$item->title.'</p>'; } i tried this $imageUrl = $item->enclosure->@attributes->url; print_r2($imageUrl); but then i get this error: Parse error: syntax error, unexpected '@', expecting T_STRING or T_VARIABLE or '{' or '$' in / this is the XML: [0] => SimpleXMLElement Object ( [title] => #3315: Woord van het jaar: weigerambtenaar, Arabische lente tweede, plaszak derde [link] => http://www.nrc.nl/nieuws/2011/11/26/woord-van-het-jaar-weigerambtenaar/?utm_campaign=rss&utm_source=syndication [pubDate] => Sat, 26 Nov 2011 17:00:55 +0100 [guid] => nieuws_153690 [description] => SimpleXMLElement Object ( ) [enclosure] => SimpleXMLElement Object ( [@attributes] => Array ( => http://www.nrc.nl/wp-content/uploads/2011/11/vmBruidstaart5501_63103-586x4801-300x245.jpg [length] => [type] => image/jpeg ) ) ) Link to comment https://forums.phpfreaks.com/topic/251836-get-attribute/ Share on other sites More sharing options...
Laash Posted November 26, 2011 Share Posted November 26, 2011 Have you tried to remove the @ sign? Link to comment https://forums.phpfreaks.com/topic/251836-get-attribute/#findComment-1291377 Share on other sites More sharing options...
clankill3r Posted November 26, 2011 Author Share Posted November 26, 2011 yes no errors the, but neither a url. Link to comment https://forums.phpfreaks.com/topic/251836-get-attribute/#findComment-1291399 Share on other sites More sharing options...
silkfire Posted November 27, 2011 Share Posted November 27, 2011 Attributes is a function in SimpleXML, mate! $imageUrl = (string)($item->enclosure->attributes()->url); Link to comment https://forums.phpfreaks.com/topic/251836-get-attribute/#findComment-1291543 Share on other sites More sharing options...
clankill3r Posted November 27, 2011 Author Share Posted November 27, 2011 thanks mate! How can i check if the atribute exists? In some cases it's not there and then i get: Warning: main() [function.main]: Node no longer exists in / Link to comment https://forums.phpfreaks.com/topic/251836-get-attribute/#findComment-1291548 Share on other sites More sharing options...
silkfire Posted November 27, 2011 Share Posted November 27, 2011 You check if the result exists: if (isset($node->attributes()->attribute)) [...] Link to comment https://forums.phpfreaks.com/topic/251836-get-attribute/#findComment-1291550 Share on other sites More sharing options...
clankill3r Posted November 27, 2011 Author Share Posted November 27, 2011 I asume that in my case it would be if (isset($item->enclosure->attributes()->url)) { but this produces still the warning Link to comment https://forums.phpfreaks.com/topic/251836-get-attribute/#findComment-1291582 Share on other sites More sharing options...
clankill3r Posted November 27, 2011 Author Share Posted November 27, 2011 found it, i have to do it like this: if (isset($item->enclosure)){ if (isset($item->enclosure->attributes()->url)){ $imageUrl = (string)($item->enclosure->attributes()->url); } } Link to comment https://forums.phpfreaks.com/topic/251836-get-attribute/#findComment-1291650 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.