我正在尝试从musixmatch api中获取数据,已经成功连接成功,但正努力从数字索引的stdclass对象打印值
这是我的json的一个结果:
{
"message": {
"header": {
"status_code": 200,
"execute_time": 0.016516923904419,
"available": 685
},
"body": {
"track_list": [
{
"track": {
"track_id": 13799336,
"track_mbid": "cc72094f-baa3-4017-b628-d95bf1087144",
"track_isrc": "",
"track_spotify_id": "",
"track_soundcloud_id": "",
"track_xboxmusic_id": "",
"track_name": "Accents",
"track_name_translation_list": [
],
"track_rating": 1,
"track_length": 238,
"commontrack_id": 9433434,
"instrumental": 0,
"explicit": 0,
"has_lyrics": 0,
"has_lyrics_crowd": 0,
"has_subtitles": 0,
"has_richsync": 0,
"num_favourite": 0,
"lyrics_id": 0,
"subtitle_id": 0,
"album_id": 13799348,
"album_name": "X-Posed: The Interview",
"artist_id": 33491916,
"artist_mbid": "e0140a67-e4d1-4f13-8a01-364355bee46e",
"artist_name": "Justin Bieber",
"album_coverart_100x100": "http:\/\/s.mxmcdn.net\/images-storage\/albums\/nocover.png",
"album_coverart_350x350": "",
"album_coverart_500x500": "",
"album_coverart_800x800": "",
"track_share_url": "https:\/\/www.musixmatch.com\/lyrics\/Justin-Bieber\/Accents?utm_source=application&utm_campaign=api&utm_medium=Song+Find%3A1409613795717",
"track_edit_url": "https:\/\/www.musixmatch.com\/lyrics\/Justin-Bieber\/Accents\/edit?utm_source=application&utm_campaign=api&utm_medium=Song+Find%3A1409613795717",
"commontrack_vanity_id": "Justin-Bieber\/Accents",
"restricted": 0,
"first_release_date": "2011-04-05T00:00:00Z",
"updated_time": "2017-04-28T14:06:57Z",
"primary_genres": {
"music_genre_list": [
]
},
"secondary_genres": {
"music_genre_list": [
]
}
}
}
]
}
}
}
这是我尝试获取track_name
$url = file_get_contents("http://api.musixmatch.com/ws/1.1/track.search?q_artist=justin%20bieber&apikey=MYAPIKEY");
$json = json_decode($url);
foreach($json->message->body->track_list->track as $track) {
echo $track->track_name;
}
上面的代码不会打印任何内容,但是如果我可以打印
$tracks = $json->message->body->track_list['0']->track->track_name;
它将打印轨道名称,但是我当然想打印列表中每个轨道的所有track_name值
任何帮助表示赞赏
答案 0 :(得分:0)
您需要如下遍历track_list数组:
$url = file_get_contents("http://api.musixmatch.com/ws/1.1/track.search?q_artist=justin%20bieber&apikey=MYAPIKEY");
$json = json_decode($url);
foreach($json->message->body->track_list as $track_list) {
echo $track_list->track->track_name;
}