title: Reducing Server Response Time Where Loads Many Resources
body: <p>I have a two PHP scripts that are loading many variable resources from APIs, causing the response times to as long as 2.2 seconds to 4 seconds.  Any suggestions on how to decrease response times and increase efficiency would be very appreciated?</p>
  
  <p><strong>FIRST SCRIPT</strong> </p>
  
  <pre><code>require('path/to/local/API_2');
  
  //Check if user has put a query and that it's not empty
  if (isset($_GET['query']) &amp;&amp; !empty($_GET['query'])) {
      //$query is user input
      $query = str_replace(" ", "+", $_GET['query']);
      $query = addslashes($query);
  
      //HTTP Request to API_1
      //Based on $query
      //Max Variable is ammount of results I want to get back in JSON format
      $varlist = file_get_contents("http://ADRESS_OF_API_1.com?$query&amp;max=10");
  
      //Convert JSON to Array()
      $varlist = json_decode($varlist, true);
  
      //Initializing connection to API_2
      $myAPIKey = 'KEY';
      $client = new APIClient($myAPIKey, 'http://ADRESS_OF_API_2.com');
      $Api = new API_FUNCTION($client);
  
      $queries = 7;
      //Go through $varlist and get data for each element in array then use it in HTML
      //Proccess all 8 results from $varlist array()
      for ($i = 0; $i &lt;= $queries; ++$i) {
  
          //Get info from API based on ID included in first API data
          //I don't use all info, but I can't control what I get back.
          $ALL_INFO = $Api-&gt;GET_FUNCTION_1($varlist[$i]['id']);
  
          //Seperate $ALL_INFO into info I use
          $varlist[$i]['INFO_1'] = $ALL_INFO['PATH_TO_INFO_1'];
          $varlist[$i]['INFO_2'] = $ALL_INFO['PATH_TO_INFO_2'];
  
          //Check if info exists
          if($varlist[$i]['INFO_1']) {
          //Concatenate information into HTML
              $result.='
                  &lt;div class="result"&gt;
                      &lt;h3&gt;'.$varlist[$i]['id'].'&lt;/h3&gt;
                      &lt;p&gt;'.$varlist[$i]['INFO_1'].'&lt;/p&gt;
                      &lt;p&gt;'.$varlist[$i]['INFO_2'].'&lt;/p&gt;
                  &lt;/div&gt;';
          } else {
              //In case of no result for specific Info ID increase
              //Allows for 3 empty responses
              ++$queries;
          }
      }
  } else {
      //If user didn't enter a query, relocates them back to main page to enter one.
      header("Location: http://websitename.com");
      die();
  }`
  </code></pre>
  
  <blockquote>
    <p>NOTE: $result equals HTML information from each time arround the loop.</p>
    
    <p>NOTE: Almost all time is spent in the <code>for ($i = 0; $i &lt;= 7; ++$i)</code>
    loop.</p>
  </blockquote>
  
  <p><strong>SECOND SCRIPT</strong> </p>
  
  <pre><code>//Same API as before 
  require('path/to/local/API_2');
  
  //Check if query is set and not empty
  if (isset($_GET['query']) &amp;&amp; !empty($_GET['query'])) {
      //$query is specific $varlist[$i]['id'] for more information on that data
      $query['id'] = str_replace(" ", "+", $_GET['query']);
      $query['id'] = addslashes($query['id']);
  
      //Initializing connection to only API used in this script
      $myAPIKey = 'KEY';
      $client = new APIClient($myAPIKey, 'http://ADRESS_OF_API_2.com');
      $Api = new API_FUNCTION($client);
  
      $ALL_INFO_1 = $Api-&gt;GET_FUNCTION_1($query['id']);
      $query['INFO_ADRESS_1.1'] = $ALL_INFO_1['INFO_ADRESS_1'];
      $query['INFO_ADRESS_1.2'] = $ALL_INFO_2['INFO_ADRESS_2'];
      $ALL_INFO_2 = $Api-&gt;GET_FUNCTION_2($query['id']);
      $query['INFO_ADRESS_2.1'] = $ALL_INFO_3['INFO_ADRESS_3'];
      $ALL_INFO_3 = $Api-&gt;GET_FUNCTION_3($query['id']);
      $query['INFO_ADRESS_3.1'] = $ALL_INFO_4['INFO_ADRESS_4'];             
      $ALL_INFO_4 = $Api-&gt;GET_FUNCTION_4($query['id']);
      $query['INFO_ADRESS_4.1'] = $ALL_INFO_5['INFO_ADRESS_5'];
      $query['INFO_ADRESS_4.2'] = $ALL_INFO_6['INFO_ADRESS_6'];
      $ALL_INFO_5 = $Api-&gt;GET_FUNCTION_5($query['id']);
      $query['INFO_ADRESS_5.1'] = $ALL_INFO_7['INFO_ADRESS_7'];
  }
  
  $result = All of the $query data from the API;
  } else {
      //If no query relocates them back to first PHP script page to enter one.
      header("Location: http://websitename.com/search");
      die();
  }`
  </code></pre>
  
  <blockquote>
    <p>NOTE: Similiarly to the first script, most time is spent getting info
    from  the secondary API.</p>
    
    <p>NOTE: In the second script, the first API is replaced by a single
    specific variable from the first script page,so $varlist[$i]['id'] =
    $query['id'].</p>
    
    <p>NOTE: Again, $result is the HTML data.</p>
  </blockquote>