这部分:
$response_2 = json_decode($response_2, true);
...下面的代码在浏览器中按字面意思回显为“数组”。如果删除该部分,则完整的$ response_2将以JSON格式在浏览器中回显,就像在此示例中一样:https://developer.spotify.com/web-api/get-list-users-playlists/
怎么回事?
<?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 = curl_exec($ch);
curl_close($ch);
$response = json_decode($response, true);
$token = $response['access_token'];
echo "My token is: " . $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);
echo $response_2;
?>
答案 0 :(得分:2)
在数组上使用echo
时,它只打印出文字字符串Array
。这只是PHP的一个怪癖。
如果要打印出阵列的内容,可以使用print_r()
或var_dump()
。
然而,看起来你真正想要做的就是打印JSON,这是字符串。 $response_2
已经是一个字符串,所以打印出来。