2016-08-01 5 views
0

私はAPIからデータを取得する方法を改善したいと考えています。この場合、私はSteam APIからすべてのapp-idを取り出し、1行に1つずつ.txtファイルでリストしたいと考えています。みんなをフェッチするには無限の(または非常に高い)ループ(すべての反復の後で++)が必要ですか?つまり、id 0から数えて、例えばforeach -loopとなるでしょうか?私は年齢がかかり、悪い習慣のように聞こえると思っています。json api配列からPHPを使用してすべて取得する

http://api.steampowered.com/ISteamApps/GetAppList/v0001の応答からすべてのappidを{"appid:" n}にするにはどうすればよいですか?

<?php 
    //API-URL 
    $url = "http://api.steampowered.com/ISteamApps/GetAppList/v0001"; 
    //Fetch content and decode 
    $game_json = json_decode(curl_get_contents($url), true); 

    //Define file 
    $file = 'steam.txt'; 
    //This is where I'm lost. One massive array {"app": []} with lots of {"appid": n}. 
    //I know how to get one specific targeted line, but how do I get them all? 
    $line = $game_json['applist']['apps']['app']['appid'][every single line, one at a time] 
    //Write to file, one id per line. 
    //Like: 
    //5 
    //7 
    //8 
    //and so on 
    file_put_contents($file, $line, FILE_APPEND); 
?> 

ちょうど右方向への任意のポインティングがはるかに理解されるであろう。ありがとう!

+0

哀れな試みだけでも、非常に良い試みをしなかったことを非常に残念に思います。なぜなら私は。 –

+0

foreachループを使用するカウンタはまったく必要ありません。 – rjdown

+2

jsonは非常に簡単です。リストを抽出するだけです! $ line = $ game_json ['applist'] ['apps'] ['app']。それでおしまい ! – cpugourou

答えて

2

foreachループでカウンタを心配する必要はありません。これらは、オブジェクト内のアイテムを通過して動作するように設計されています。

$file  = "steam.txt"; 
$game_list = ""; 
$url  = "http://api.steampowered.com/ISteamApps/GetAppList/v0001"; 
$game_json = file_get_contents($url); 
$games  = json_decode($game_json); 

foreach($games->applist->apps->app as $game) { 
    // now $game is a single entry, e.g. {"appid":5,"name":"Dedicated server"} 
    $game_list .= "$game->appid\n"; 
} 

file_put_contents($file, $game_list); 

ここには、28000個の数字を含むテキストファイルがあります。おめでとう?

関連する問題