I am using this php library to communicate with the spotify api to get my user details. The spotify api returns user data which I then want to add to a json file. Basically, whatever is sent back from the api I want to append to the json file for each user.
The data returned from the api looks like the following when I do print_r($api->me());
This is basically coming from this api call.
stdClass Object ( [display_name] => Paul Flanagan
[email] => paulflanagan@gmail.com
[external_urls] => stdClass Object (
[spotify] => https://open.spotify.com/user/21aydctlhgjst3z7saj2rb4pq ) [followers] => stdClass Object (
[href] => [total] => 19 )
[href] => https://api.spotify.com/v1/users/2391231jasdasd1
[id] => 21aydctlhgjst3z7saj2rb4pq
[images] => Array ( [0] => stdClass Object (
[height] => [url] => https://scontent.xx.fbcdn.net/v/t1.0-1/p200x200/18301863_452622995075637_5517698155320169855_n.jpg?oh=9e949fafd3ee84705ea5c1fa1aa9c811&oe=59C9F63C
[width] => ) ) [type] => user [uri] => spotify:user:21aydctlhgjst3z7saj2rb4pq )
I want to write this code to a json file
I have attempted many approaches but as I am more javascript focused than php I am struggling to write the data correctly. My latest attempt at the code looks like this:
<?php
require 'vendor/autoload.php';
$session = new SpotifyWebAPI\Session(
'KEY1',
'KEY2',
'CALLBACK_URL'
);
$api = new SpotifyWebAPI\SpotifyWebAPI();
if (isset($_GET['code'])) {
$session->requestAccessToken($_GET['code']);
$api->setAccessToken($session->getAccessToken());
$file = "users.json";
$json = json_decode(file_get_contents($file), true);
$file = fopen($file,'w+');
fwrite($file, $api->me());
fclose($file);
print_r($api->me());
} else {
$options = [
'scope' => [
'user-read-email',
],
];
header('Location: ' . $session->getAuthorizeUrl($options)$
die();
}
?>