title: load PHP: file_get_contents after html loads
body: <p>I'm trying to change working site to load the php <code>file_get_contents</code> after the html fully loads. The existing code works fine but due to the large amount of data captured from 3 different sites, its a long delay that sometimes times out the page - I don't want to extend the timeout as others have suggested, I just need to content to load in background after the html loads - other problem may be that index.php (main HTML Page) is a single page with collapsable divs containing the file data.</p>
  
  <pre><code>&lt;a href="#hx" class="list-group-item" data-toggle="collapse"&gt;MC CHATS ()&lt;span class="badge"&gt;7&lt;/span&gt;&lt;/a&gt;
              &lt;div class="container"&gt;
                  &lt;div id="hx" class="collapse"&gt;
                      &lt;h1&gt;MC CHATS&lt;/h1&gt;
                          &lt;h3&gt;Region V (Hospitals)&lt;/h2&gt;
                      &lt;div class="list-group"&gt;
  
  &lt;?php
  session_start();
  
  $use_sts = true;
  
  if ($use_sts &amp;&amp; isset($_SERVER['HTTPS']) &amp;&amp; $_SERVER['HTTPS'] != 'off') {
      header('Strict-Transport-Security: max-age=31536000');
  } elseif ($use_sts) {
      header('Location: https://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'].$_SERVER['QUERY_STRING'], true, 301);
  die();
  }
  
  $data = file_get_contents('https://www.example.com/region5.php');
  $json = json_decode($data, true);
  $selected = array(5,6,7,10,12,15,17,18,22,23);
      //$selected = array();
  
  foreach ($json as $key =&gt; $value) {
      if (in_array($key, $selected) || empty($selected)) {
          echo '&lt;a href="#hx" class="list-group-item clearfix" data-toggle="collapse"&gt;' . $value["name"] . '&lt;br&gt;'; 
          $length = count($value["color"]);
          for ($i = 0; $i &lt; $length; $i++) {
              if($value['time'][$i] != "") {
                  echo '&lt;span class="badge pull-left" style="' . $value["color"][$i] . '"&gt;' . $value["time"][$i] . '&lt;/span&gt;'; 
              }
          }
          echo '&lt;/a&gt;';
      }
  }
  
  $data = file_get_contents('https://www.example.com/region124.php');
  $json = json_decode($data, true);
  $selected = array(2);
  
  foreach ($json as $key =&gt; $value) {
      if (in_array($key, $selected) || empty($selected)) {
          echo '&lt;a href="#hx" class="list-group-item clearfix" data-toggle="collapse"&gt;' . $value["name"] . '&lt;br&gt;'; 
          $length = count($value["color"]);
          for ($i = 0; $i &lt; $length; $i++) {
              if($value['time'][$i] != "") {
                  echo '&lt;span class="badge pull-left" style="' . $value["color"][$i] . '"&gt;' . $value["time"][$i] . '&lt;/span&gt;'; 
              }
          }
          echo '&lt;/a&gt;';
      }
  }
  
  
  $data = file_get_contents('https://www.example.com/region3.php');
  $json = json_decode($data, true);
  $selected = array(3,9,16);
  
  foreach ($json as $key =&gt; $value) {
      if (in_array($key, $selected) || empty($selected)) {
          echo '&lt;a href="#hx" class="list-group-item clearfix" data-toggle="collapse"&gt;' . $value["name"] . '&lt;br&gt;'; 
          $length = count($value["color"]);
          for ($i = 0; $i &lt; $length; $i++) {
              if($value['time'][$i] != "") {
                  echo '&lt;span class="badge pull-left" style="' . $value["color"][$i] . '"&gt;' . $value["time"][$i] . '&lt;/span&gt;'; 
                  if($value["color"][$i] = "background-color:#ff6600;color:#000000;"){
                  }
  
              }
          }
          echo '&lt;/a&gt;';
      }
  }
  
  $data = file_get_contents('https://www.example.com/region_blue.php');
  $json = json_decode($data, true);
  $selected = array(2);
  
  foreach ($json as $key =&gt; $value) {
      if (in_array($key, $selected) || empty($selected)) {
          //echo '&lt;br&gt;&lt;a href="#hx" class="list-group-item clearfix" data-toggle="collapse"&gt;' . $value["name"] . ' (Current Condition Color/Status)&lt;br&gt;'; 
          $length = count($value["color"]);
          for ($i = 0; $i &lt; $length; $i++) {
              if($value['time'][$i] != "") {
                  echo '&lt;br&gt;&lt;a href="#hx" class="list-group-item clearfix" data-toggle="collapse"&gt;' . $value["name"] . ' (Current Condition Color/Status)&lt;br&gt;'; 
                  echo '&lt;span class="badge pull-left" style="' . $value["color"][$i] . '"&gt;' . $value["time"][$i] . '&lt;/span&gt;'; 
              }
          }
          echo '&lt;/a&gt;';
      }
  }
  
  
  function myArray($arry) {
      $selected = array(5,6,7,10,12,15,17,18,22,24);
      $color = $time = $emailbody = ""; // &lt;== initialising with empty strings
      foreach($arry as $key =&gt; $value) {
          if (in_array($key, $selected) || empty($selected)) {
              $color .= $value['name']. chr(10);
              $time .= $value['name']. chr(10);
              $emailbody .= $value['name']. '&lt;br&gt;';
              $length = count($value["color"]);
              /*
              // TEST of color alert
              print($color + '&lt;br&gt;' + chr(10) + '&lt;br&gt;');
              print($time + '&lt;br&gt;');
              print($emailbody + '&lt;br&gt;');
              print($length + '&lt;br&gt;');
              // end
              */
              for ($i = 0; $i &lt; $length; $i++) {
                  if($value['time'][$i] != "") {      
  
                      if(preg_match('/#ff0000/', $value['color'][$i])) {
                          $value['color'][$i] = '&lt;font color="#ff0000"&gt;RED&lt;/font&gt;';
                      }
                      if(preg_match('/#ffff00/', $value['color'][$i])) {
                          $value['color'][$i] = '&lt;font color="#ffff00"&gt;YELLOW&lt;/font&gt;';
                      }
                      if(preg_match('/#006600/', $value['color'][$i])) {
                          $value['color'][$i] = '&lt;font color="#006600"&gt;GREEN (Mini Disaster)&lt;/font&gt;';
                      }
                      if(preg_match('/#9933cc/', $value['color'][$i])) {
                          $value['color'][$i] = '&lt;font color="#9933cc"&gt;PURPLE (Trauma ByPass)&lt;/font&gt;';
                      }
                      if(preg_match('/#ff6600/', $value['color'][$i])) {
                          $value['color'][$i] = '&lt;font color="#ff6600"&gt;ORANGE (ReRoute)&lt;/font&gt;';
                      }
  
                      $time .= '  &lt;b&gt;' . $value["color"][$i] . '&lt;/b&gt; - &lt;i&gt;' . $value["time"][$i] . '&lt;/i&gt;' . chr(10); 
                      $color .= '  &lt;b&gt;' . $value["color"][$i] . '&lt;/b&gt;' . chr(10); 
                      $emailbody .= '  &lt;b&gt;' . $value["color"][$i] . '&lt;/b&gt; - &lt;i&gt;' . $value["time"][$i] . '&lt;/i&gt;&lt;br&gt;';
  
                  }
              }
          }
      }
      return array($time, $color, $emailbody);
  }
  
  ?&gt;
  
  &lt;/div&gt;
  </code></pre>