2017-06-22 18 views
0

私はthis php libraryを使用して、ユーザーの詳細を取得するためにspotify apiと通信しています。 spotify apiはユーザーデータを返し、jsonファイルに追加します。基本的には、apiから返されるものは何でも、私は各ユーザのjsonファイルに追加したいと思います。jsonファイルにデータを追加する

私が行ったとき、APIから返されたデータは次のようになりますprint_r($api->me());これは基本的にこのapi callから来ています。

stdClass Object ([display_name] => Paul Flanagan 
[email] => [email protected] 
[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) 

は、私は多くのアプローチを試みてきたJSONファイル

にこのコードを書きたいが、私はより多くのjavascriptのは、PHPよりも重視していますように私は正確にデータを書き込むのに苦労しています。コードでの私の最新の試みは、次のようになります。

<?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(); 

    } 
?> 
+0

'$ api-> me()'は何を返しますか? –

+0

ちょっと@u_mulder上記を参照してください。これは最初のスニペットです - これは 'stdClass Object([display_name] => Paul Flanagan ...' –

+0

のように見えます)ファイルに書き込むとどうなりますか? –

答えて

1

object戻り$api->me()として - あなたが直接ファイルに書き込むことはできません。オブジェクトをstringに変換する必要があります。簡単な方法はjson_encodeです。

$file = "users.json"; 
$json = json_decode(file_get_contents($file), true); 
$file = fopen($file,'w+'); 
fwrite($file, json_encode($api->me())); 
fclose($file); 

次の問題は、データを上書きしています。 w+でファイルを開くと、ファイルは0の長さに切り捨てられます。

ソリューションは、以前のデータで必要なものによって異なります。もしあなたがいくつかのデータを書き直したいのであれば、私は現在の行動がすでにそれをしていると思います。

データをファイルに追加する場合は、ファイルを開くときにanother modeを使用する必要があります(例:a+)。しかし、この場合、ファイルの内容は正しいjsonではないでしょう.1つのjson文字列ではなく、であるの正しいjsonという文字列をファイルに書き出すためです。したがって、適切な解決策を見つけることはあなた次第です。

更新:

は、ファイル名によると、私はあなたがそれでユーザーを格納するとします。だから、jsonでエンコードされたユーザーのリストがあると思う。だから、簡単な解決策は次のようになります:

$file = "users.json"; 
$json = json_decode(file_get_contents($file), true); 

// Now $json stores list of you current users. I suppose it's a simple array of arrays 

// This is data for a new user 
$new_user = $api->me(); 

// as data `$new_user` is object, I think you need to convert it to array 
// this can be done as: 
$new_user = json_decode(json_encode($new_user), true); 

// now, add new user to existsing array 
$json[] = $new_user; 

$file = fopen($file,'w+'); 

// now we can encode `$json` back and write it to a file 
fwrite($file, json_encode($json)); 
fclose($file); 
+0

リストがあれば、ファイル内のユーザーの数。 –

関連する問題