Token aus Antwort-URL extrahieren - Spotify-API
Mit diesem Code erhalte ich ein Token von der 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);
?>
Dies führt dazu, dass im Browser Folgendes angezeigt wird:
{"access_token":"{token}","token_type":"Bearer","expires_in":3600}
Toll! Aber wie extrahiere ich "{Token}" aus der Antwort und verwende es als Parameter in einer Anfrage an die API? Zum Beispiel in der Anfrage anhttps://api.spotify.com/v1/users/{user_id} / Wiedergabelisten, die das Token im Headerfeld benötigen.
Vielen Dank!