从php api响应创建的动态表

时间:2016-02-24 01:11:23

标签: php

我有一个调用api的php文件,并使用file_get_contentsjson_decode将其存储到数组中。我有一个纯文本文件,其中包含脚本每次加载的API调用的唯一IDS。这些调用中的每一个都需要大约1-6秒,具体取决于API的速度。我使用foreach进行呼叫/存储和显示。加载这个大约需要10-30秒左右这是否有一些显示这些结果,因为它们被调用和处理,所以页面似乎没有反应?例如,用户将看到一次创建一个插槽而不是批量加载的表。这是我用来获取和显示数据的功能,我还在学习php,所以对于比我更了解的人来说,这看起来很可怕。

function GetInfo(){
   $data = file('http://site', FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);


echo '<body>
       <div class="container">           
         <table class="table">
           <thead>
             <tr>
              <th>Name</th>
              <th>Status</th>
              <th>Game</th>
              <th>Current Server</th>
             </tr>
           </thead>
           <tbody>';

          foreach ($data as $xy){
            $api = "http://api.steampowered.com/ISteamUser/GetPlayerSummaries/v0002/?key=snip&steamids=" . $xy;
            $json = file_get_contents($api);
            $decoded = json_decode($json);
            $test = ($decoded->response->players[0]->personaname);
            $serverip = ($decoded->response->players[0]->gameserverip);
            $persona = ($decoded->response->players[0]->personastate);
            $servername = server($serverip);
            $visibility = ($decoded->response->players[0]->communityvisibilitystate);
            $gamename = ($decoded->response->players[0]->gameextrainfo);

            if ($visibility == 1 )
               $persona = "7";
               switch ($persona) {
                  case 0:
                    $colorcode = "danger";
                    $status = "Offline";
                  break;
                  case 1:
                    $colorcode = "success";
                    $status = "Online";
                  break;
                  case 2:
                    $status = "Busy";
                    $colorcode = "info";
                  break;
                  case 3:
                    $status = "Away";
                    $colorcode = "info";
                  break;
                  case 4:
                    $status = "Snooze";
                    $colorcode = "info";
                  break;
                  case 5:
                    $status = "Looking to trade";
                    $colorcode = "danger";
                  break;
                  case 6:
                    $status = "Looking to play";
                    $colorcode = "danger";
                  break;
                  case 7:
                    $status = "PRIVATE";
                    $colorcode = "danger";
                  break;
               }

               echo '<tr class="'. $colorcode .'">
                       <td><a href="http://steamcommunity.com/profiles/'.$xy.'">'.$test.'</a></td>
                       <td>'.$status.'</td>
                       <td>'.$gamename.'</td>
                       <td>'.$servername.'</td>
                     </tr>';
          }
         echo  '</tbody>
         </table>
       </div>

    </body>
    </html>';
}

1 个答案:

答案 0 :(得分:0)

我使用ob_flush();flush();来输出缓冲区,它似乎有效,如果有人有一个我还在观看的更好的解决方案,这就是我找到的解决方案。