Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
561 views
in Technique[技术] by (71.8m points)

xml - How get values from MusicBrainz simplexml_load_file() in php?

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 ([email protected])') ;
$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 ([email protected])') ;
    $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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
等待大神答复

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...