This part:
$response_2 = json_decode($response_2, true);
...of the code below is echoed literally as "Array" in the browser. If I remove the part, the full $response_2 is echoed in browser in JSON-format just like in this example: https://developer.spotify.com/web-api/get-list-users-playlists/
How come?
<?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;
?>
json_decode()returns an array. You can't echo an array. Useprint_r()orvar_dump()to inspect the contents. And then echo a specific element, like so:echo $response_2['foo'];.print_r ($response_2);and it worked great.print_r()is just for debugging; it is not meant to be used in actual code. @Rawlandforeachloop.foreach($response_2['items'] as $item) {echo 'Title: ' . $item['external_urls'] . '<br />'; echo 'Brand: ' . $item['owner']['external_urls'] . '<br />'; }If replace "echo" with "print_r" nothing happens. Am I going about this all wrong?