【发布时间】:2014-08-22 13:48:47
【问题描述】:
我正在使用此代码从 Spotify 的 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);
?>
这会在浏览器中显示:
{"access_token":"{token}","token_type":"Bearer","expires_in":3600}
太棒了!但是如何从响应中提取“{token}”并将其用作对 API 的请求中的参数?例如在对https://api.spotify.com/v1/users/{user_id}/playlists 的请求中,它需要标头字段中的令牌。
谢谢!
【问题讨论】:
标签: php api curl token spotify