Extract token from response url - Spotify API
Asked Answered
P

2

2

I'm using this code to get a token from Spotify's Web API:

<?php
$url = 'https://accounts.spotify.com/api/token';
$method = 'POST';

$credentials = "{Client ID}:{Client Secret}";

$headers = array(
        "Accept: */*",
        "Content-Type: application/x-www-form-urlencoded",
        "User-Agent: runscope/0.1",
        "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);

$response = curl_exec($ch);
?>

That results in this showing up in the browser:

{"access_token":"{token}","token_type":"Bearer","expires_in":3600}

Great! But how do I extract "{token}" from the response and use it as a parameter in a request to the API? For example in the request to https://api.spotify.com/v1/users/{user_id}/playlists which needs the token in the header field.

Thanks!

Prue answered 1/7, 2014 at 21:49 Comment(0)
N
1

You need to decode the JSON:

$response = json_decode($response, true);

Then you'll have an array with the values.

$token = $response['access_token'];

Also, you're missing a necessary option to obtain the response in this way:

curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

If not defined, you will get a boolean value instead of the response.

Neutralization answered 1/7, 2014 at 21:53 Comment(10)
I added those two lines at the very bottom then put "echo $token;" after that, to verify that "$token" is the value I want, but it doesn't show up in my browser. What am doing wrong? Thanks!Prue
Do var_dump($response); before and after json_decode.Neutralization
I'm sorry, but I can't get it right. I don't really need to show the token value in the browser. I want to extract {token} from the response and use it as a value in my calls to the API. Exactly where in my code should I put your two original lines and how can I verify that it's working? Could you put your rows of code in my code? Thanks!Prue
var_dump is only to debug your code and verify that it's working. The two answer's lines should be put after $response = curl_exec($ch); What do you see with var_dump?Neutralization
Here's the code when I've added your lines: http://codeshare.io/u9aap It gives me the same result as mention in original question, but with bool(true) int(1) after it.Prue
It gives me an error now. Did I put the "var_dumps" and everything at the right place?Prue
So forget the last comment. You may have to close curl after curl_exec: curl_close($ch);. What happen if you echo the response before json_decode?Neutralization
Then it only echoes bool(true). See updated code at http://codeshare.io/CCHpV.Prue
Just use $token = $response['access_token']; and echo $token; Remove the var_dump lines.Neutralization
Thanks a lot for helping me out Manolo! It works great!Prue
H
0
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 

use this line too before your curl execute then it gives you proper json

Hyacinthie answered 1/8, 2021 at 6:14 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.