DIM3NSION Posted April 30, 2011 Share Posted April 30, 2011 Hi Guys. I'm struggling to find a way to access parts of an XML document through PHP Ive managed to use cURL to get a response from an api that lists music artists album releases - I have got the response loaded into a simpleXMLelement. printing out the code it returns an array that looks like this - [2] => SimpleXMLElement Object ( [@attributes] => Array ( [num] => 3 [type] => artist ) [title] => DJ Tiësto [uri] => http://www.discogs.com/artist/DJ+Ti%C3%ABsto [summary] => DJ Tiësto Tijs Michiel Verwest Dutch trance DJ & producer (there are multiple entries in the array with different content types) What i'm trying to achieve is to parse this xml code so that all the array items that have the attribute type set to artist then display the summary code for it. This is the full PHP code i have but it doesn't return anything. <?php $curl = curl_init(); curl_setopt($curl, CURLOPT_URL, "http://www.discogs.com/search?type=all&" . "q=DJ+Tiësto&" . "f=xml&" . "api_key=<APIKEY>"); curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); curl_setopt($curl, CURLOPT_ENCODING, "gzip"); $result = curl_exec($curl); curl_close($curl); $xmlmusic = new SimpleXMLElement($result,NULL,true); foreach ($xmlmusic as $xm) { $attrs = $xm->attributes(); if($attrs["type"] == "title") echo $xm->summary."\n"; } ?> Am i going in the right direction? Quote Link to comment https://forums.phpfreaks.com/topic/235216-using-curl-and-parsing-parts-of-xml-array/ Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.