【发布时间】:2014-08-23 18:06:03
【问题描述】:
我的网站调用 Spotify Web API 并获取给定用户的所有公共播放列表。响应最初是 JSON,但我的代码对其进行了解码。
我想要代码做的下一件事是只显示对象 [external_urls]、[name] 和 [tracks] 对于响应中的每个项目。为此,我已经尝试过:
foreach($response_2['items'] as $item) {
echo 'Link: ' . $item['external_urls'] . '<br />';
echo 'Name: ' . $item['name'] . '<br />';
echo 'Tracks: ' . $item['tracks'] . '<br />'; }
...但它不能正常工作。所有 [name] 对象(即a string)都正确回显,但对于所有 [external_urls] 和 [tracks](其中是objects) 它只在浏览器中显示“数组”。
我想我应该对这些对象使用 print_r(),但我无法让它工作。
有什么建议吗?
我的代码:
<?php
$url = 'https://accounts.spotify.com/api/token';
$method = 'POST';
$credentials = "hidden:hidden";
$headers = array(
"Accept: */*",
"Content-Type: application/x-www-form-urlencoded",
"Authorization: Basic " . base64_encode($credentials));
$data = 'grant_type=client_credentials';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response_1 = curl_exec($ch);
curl_close($ch);
$response_1 = json_decode($response_1, true);
$token = $response_1['access_token'];
$headers_2 = array(
"Accept: */*",
"Content-Type: application/x-www-form-urlencoded",
('Authorization: Bearer ' . $token));
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,'https://api.spotify.com/v1/users/wizzler/playlists');
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers_2);
curl_setopt($ch, CURLOPT_POST, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response_2 = curl_exec($ch);
curl_close($ch);
$response_2 = json_decode($response_2, true);
?>
API 响应:
{
"href": "https://api.spotify.com/v1/users/wizzler/playlists",
"items": [
{
"collaborative": false,
"external_urls": {
"spotify": "http://open.spotify.com/user/wizzler/playlists/53Y8wT46QIMz5H4WQ8O22c"
},
"href": "https://api.spotify.com/v1/users/wizzler/playlists/53Y8wT46QIMz5H4WQ8O22c",
"id": "53Y8wT46QIMz5H4WQ8O22c",
"name": "Wizzlers Big Playlist",
"owner": {
"external_urls": {
"spotify": "http://open.spotify.com/user/wizzler"
},
"href": "https://api.spotify.com/v1/users/wizzler",
"id": "wizzler",
"type": "user",
"uri": "spotify:user:wizzler"
},
"public": true,
"tracks": {
"href": "https://api.spotify.com/v1/users/wizzler/playlists/53Y8wT46QIMz5H4WQ8O22c/tracks",
"total": 30
},
"type": "playlist",
"uri": "spotify:user:wizzler:playlist:53Y8wT46QIMz5H4WQ8O22c"
},
{
"collaborative": false,
"external_urls": {
"spotify": "http://open.spotify.com/user/wizzlersmate/playlists/1AVZz0mBuGbCEoNRQdYQju"
},
"href": "https://api.spotify.com/v1/users/wizzlersmate/playlists/1AVZz0mBuGbCEoNRQdYQju",
"id": "1AVZz0mBuGbCEoNRQdYQju",
"name": "Another Playlist",
"owner": {
"external_urls": {
"spotify": "http://open.spotify.com/user/wizzlersmate"
},
"href": "https://api.spotify.com/v1/users/wizzlersmate",
"id": "wizzlersmate",
"type": "user",
"uri": "spotify:user:wizzlersmate"
},
"public": true,
"tracks": {
"href": "https://api.spotify.com/v1/users/wizzlersmate/playlists/1AVZz0mBuGbCEoNRQdYQju/tracks",
"total": 58
},
"type": "playlist",
"uri": "spotify:user:wizzlersmate:playlist:1AVZz0mBuGbCEoNRQdYQju"...
}
],
"limit": 9,
"next": null,
"offset": 0,
"previous": null,
"total": 9
}
【问题讨论】:
-
您之前的问题也适用于此。无法使用
echo输出对象和数组。对对象和数组使用var_dump()或print_r()... -
正如我在问题中提到的那样,我已经尝试过:“我想我应该对这些对象使用 print_r(),但我无法让它工作。”。我可以混合
print_r()和echo相同的“foreach”吗?当我这样做时,当我使用它时它仍然会得到“数组”:print_r ('Link: ' . $item['external_urls'] . '<br />'); -
echo 强制将数组转换为字符串。将它与另一个字符串连接并调用 print_r after. 也是如此
标签: php json parsing object spotify