0

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;

?>
7
  • 4
    json_decode() returns an array. You can't echo an array. Use print_r() or var_dump() to inspect the contents. And then echo a specific element, like so: echo $response_2['foo'];. Commented Jul 2, 2014 at 18:09
  • Appreciated! I went with print_r ($response_2); and it worked great. Commented Jul 2, 2014 at 18:15
  • Cool. Note, however, that print_r() is just for debugging; it is not meant to be used in actual code. @Rawland Commented Jul 2, 2014 at 18:17
  • You'd need to iterate over the array with a foreach loop. Commented Jul 2, 2014 at 18:22
  • In the next step I want to this though: 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? Commented Jul 2, 2014 at 18:30

1 Answer 1

2

When you use echo on an array, it just prints out the literal string Array. This is just a quirk of PHP.

If you want to print out the contents of the array, you can use print_r() or var_dump().

However it seems like what you actually want to do is print the JSON, which is the string. $response_2 is already a string, so print it out.

Sign up to request clarification or add additional context in comments.

1 Comment

It should be noted that this applies to objects as well (in reference to the OP's duplicate question)...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.