Php 如何使用# https://accounts.spotify.com/authorize?client_id=6af26783dff4466fa5cbcce0f042aba7&redirect_uri=http://bon-deals.com/School/Collegas/Heartify/callback&scope=user read private+user read email&response\u type=token
单击上述链接时,Spotify API将响应以下示例链接:Php 如何使用# https://accounts.spotify.com/authorize?client_id=6af26783dff4466fa5cbcce0f042aba7&redirect_uri=http://bon-deals.com/School/Collegas/Heartify/callback&scope=user read private+user read email&response\u type=token,php,curl,oauth,spotify,access-token,Php,Curl,Oauth,Spotify,Access Token,单击上述链接时,Spotify API将响应以下示例链接: http://bon-deals.com/School/Collegas/Heartify/callback#access_token=BQALr_8ac05PFDIo43oPddsSJafQNXn2nEgqR0FJALJov8dJktgrSzRoAYCGijlXyLVOZYHryhvj7TXsTh6wc3ntVrOMx36HscqS_pA9b6978bMjIP9U6IWSu-aErTRwc2rScM4Y7iJtpKRsid0MGMCL
http://bon-deals.com/School/Collegas/Heartify/callback#access_token=BQALr_8ac05PFDIo43oPddsSJafQNXn2nEgqR0FJALJov8dJktgrSzRoAYCGijlXyLVOZYHryhvj7TXsTh6wc3ntVrOMx36HscqS_pA9b6978bMjIP9U6IWSu-aErTRwc2rScM4Y7iJtpKRsid0MGMCLq3tPvCQ&token_type=Bearer&expires_in=3600
我无法设法$\u获取['access\u token']
,因为URL中访问\u token
之前有
我们还尝试通过更改
response\u type=code
来获取访问令牌,然后使用cURL-POST方法将代码传输到访问令牌,但也没有成功。您无法访问URL服务器端的所谓片段,因为片段部分仅用于用户代理(即浏览器)。请求的响应类型token
表示调用方希望在片段中传递令牌,因为它是浏览器内客户端或本机应用程序
如果希望将令牌传递到服务器端后端,则需要使用其他类型的响应,例如response\u type=code
。然后,您可以通过调用Spotify的令牌端点,将code
交换为access\u令牌
如果这对您不起作用,那将是另一个问题,包括错误描述/日志等。您正确执行了卷曲吗?
你得到了什么回应?
您是否检查了HTTP响应头?
当@hans Z说“因为片段部分仅用于用户代理(即浏览器)”,这是不对的。
问题通常出现在请求头中。下面的curl代码将获得任何用户代理都能获得的所有细节。您只需要正确设置请求头。您可能会删除Cookie:
如果您需要发表文章,请添加:
$post = 'key1=value1&key2=value2&key3=value3';
curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
PHP
$request = array();
$request[] = 'Host: xxxxxxx';
$request[] = 'User-Agent: Mozilla/5.0 (Windows NT 5.1; rv:39.0) Gecko/20100101 Firefox/39.0';
$request[] = 'Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8';
$request[] = 'Accept-Language: en-US,en;q=0.5';
$request[] = 'Accept-Encoding: gzip, deflate';
$request[] = 'DNT: 1';
$request[] = 'Cookie: xxxx
$request[] = 'Connection: keep-alive';
$request[] = 'Pragma: no-cache';
$request[] = 'Cache-Control: no-cache';
$url = 'https://xxxx.com/';
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_ENCODING,"");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLINFO_HEADER_OUT, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_FILETIME, true);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 100);
curl_setopt($ch, CURLOPT_VERBOSE, true);
curl_setopt($ch, CURLOPT_AUTOREFERER, true);
curl_setopt($ch, CURLOPT_TIMEOUT,100);
curl_setopt($ch, CURLOPT_FAILONERROR,true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $request);
$data = curl_exec($ch);
if (curl_errno($ch)){
$data .= 'Retreive Base Page Error: ' . curl_error($ch);
}
else {
$skip = intval(curl_getinfo($ch, CURLINFO_HEADER_SIZE));
$responseHeader = substr($data,0,$skip);
$data= substr($data,$skip);
$info = curl_getinfo($ch);
$info = var_export($info,true);
}
echo $responseHeader . $info . $data;
$\u GET
包含请求到服务器的url的查询变量,即当前脚本。如果从脚本中的api收到响应,则需要解析响应以获取变量,这与$\u get
无关。你的代码看起来像什么?