这部分:
$ response_2 = json_decode($ response_2,true);...下面的代码在浏览器中被称为Array。如果我删除部分,完整的$ response_2在浏览器中以JSON格式回显,就像这个例子: developer.spotify/web-api/get-list-users-playlists/
如何
<?php $ url ='https://accounts.spotify。 com / api / token'; $ method ='POST'; $ credentials =hidden:hidden; $ headers = array(Accept:* / *,Content-Type:application / x-www-form-urlencoded,授权:基本。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我的令牌是:。 $ 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/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; ?>当你使用 echo在数组上,它只是打印出字符串 Array 。这只是PHP的一个奇怪。
如果要打印数组的内容,可以使用 print_r()或 var_dump()。
但是,你真正想要做的是打印JSON,这是字符串。 $ response_2 已是一个字符串,因此请将其打印出来。
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: developer.spotify/web-api/get-list-users-playlists/
How come?
<?php $url = 'accounts.spotify/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,'api.spotify/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; ?>解决方案
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.
更多推荐
JSON解码显示“Array”
发布评论