SimpleXMLElemtent not reading attributes?

I’m getting an rss feed from flickr, and using SimpleXMLElement, however it seems to not pick up on attributes?

$feedurl = "http://api.flickr.com/services/feeds/photos_public.gne?id=48839201@N07&lang=en-us&format=rss_200";
	
	$curl_handle=curl_init();
	curl_setopt($curl_handle,CURLOPT_URL,$feedurl);
	curl_setopt($curl_handle,CURLOPT_CONNECTTIMEOUT,2);
	curl_setopt($curl_handle,CURLOPT_RETURNTRANSFER,1);
	$feed = curl_exec($curl_handle);
	curl_close($curl_handle);
	
	$curl = curl_init($feedurl);
	curl_setopt_array(
	    $curl,
	    array(
	        CURLOPT_CONNECTTIMEOUT  => 5,
	        CURLOPT_RETURNTRANSFER  => true
	    )
	);
	
	if(false !== ($data = curl_exec($curl))){
	    try{
	        $xmlData = new SimpleXMLElement($data);
	    }catch(Exception $exception){
	        $xmlData = false;
	    }
	}
		
	if($xmlData != false) { 
		
		print_r($xmlData);
		
	} else {

	echo "Error";
	
	}

It doesn’t seem to get “<media:thumbnail>” or “<media:category>” and all that… any idea why?

It only picks:

[0] => SimpleXMLElement Object
(
[title] =>
[link] =>
[description] =>
[pubDate] =>
[author] =>
[guid] =>
)

*obviously filled in, I just removed all the content

I don’t see any tags with media:thumbnail in that url. It’s just not in the XML file. Have you looked at that contents of the XML file?

It’s showing it for me…?

Given your example code, here’s one way of accessing those media:thumbnail nodes.


foreach ($xmlData->channel->item as $item) {
    // Get media:* nodes for this item
    $media = $item->children("media", TRUE);
    // Get the first thumbnail node's attributes
    $thumb = $media->thumbnail[0]->attributes();
    // Print image URL and dimensions
    printf("&#37;s (%dx%d)\
", $thumb->url, $thumb->width, $thumb->height);
}

More info:
SimpleXMLElement::children for accessing namespaced children of a node.
SimpleXMLElement::attributes for accessing attributes (the $node[‘attr’] syntax doesn’t appear to be available in this case).

P.S. Don’t be concerned that you cannot see the media:* nodes via print_r or var_dump, that’s perfectly normal (though confusing at first sight) behaviour.