简体   繁体   中英

dynamic tables created from php api response

I have a php file that calls an api and uses file_get_contents and json_decode to store it into an array. I have a plain text file holding the unique IDS for the API calls that the script loads every time. Each one of these calls takes about 1-6 seconds depending on how slow the API is being. I use a foreach for the calls / storage and display. It takes roughly 10-30 seconds to load this

Is there someway to display these results as they are called and processed so the page doesn't seem so unresponsive? For example the user will see the table being created one slot at a time rather then a bulk load. This is the function I'm using to fetch and display the data, I'm still learning php so it might look hideous to people who know better than I.

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>';
}

I used ob_flush(); and flush(); to output the buffer and it appears to work, this is the solution I found if anyone has a better one I'm still watching.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM