0

I know there are topics related to MusicBrainz which has help me so far, but I am stuck on the below.

I cannot echo the values from the result

<?php 
ini_set('user_agent', 'MyApp/1.0 (music@test.com)') ;
$xml = simplexml_load_file('http://musicbrainz.org/ws/2/artist/?query=artist:eminem');
print_r($xml);
?>

PHP result sample

SimpleXMLElement Object
(
    [@attributes] => Array
        (
            [created] => 2021-01-08T14:46:14.802Z
        )

    [artist-list] => SimpleXMLElement Object
        (
            [@attributes] => Array
                (
                    [count] => 1
                    [offset] => 0
                )

            [artist] => SimpleXMLElement Object
                (
                    [@attributes] => Array
                        (
                            [id] => b95ce3ff-3d05-4e87-9e01-c97b66af13d4
                            [type] => Person
                            [type-id] => b6e035f4-3ce9-331c-97df-83397230b0df
                        )

                    [name] => Eminem
                    [sort-name] => Eminem
                    [gender] => male
                    [country] => US
                    [area] => SimpleXMLElement Object
                        (
                            [@attributes] => Array
                                (
                                    [id] => 489ce91b-6658-3307-9877-795b68554c98
                                    [type] => Country
                                    [type-id] => 06dd0ae4-8c74-30bb-b43d-95dcedf961de
                                )

                            [name] => United States
                            [sort-name] => United States
                            [life-span] => SimpleXMLElement Object
                                (
                                    [ended] => false
                                )

                        )

Now when I echo the artist name as per below I get no output

<?php 
    ini_set('user_agent', 'MyApp/1.0 (music@test.com)') ;
    $xml = simplexml_load_file('http://musicbrainz.org/ws/2/artist/?query=artist:eminem');
    //print_r($xml);
echo $xml->artist->name;
    ?>

Please can you explain what I am doing wrong I cannot seem to get any values from the xml result

Gov
  • 33
  • 3
  • The data is also under the `artist-list`, sou you would need something like `echo $xml->{'artist-list'}->artist->name;` – Nigel Ren Jan 08 '21 at 15:00
  • @NigelRen one more question how do you get values from the '@attributes' i've tried echo $xml->{'@attributes'}->created no result – Gov Jan 08 '21 at 15:12
  • Attributes are access using array notation, so `$xml['created']` style – Nigel Ren Jan 08 '21 at 15:19

0 Answers0