Jump to content

Using cURL and parsing parts of XML array


DIM3NSION

Recommended Posts

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?

 

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.