John
John

Reputation: 194

Reducing Server Response Time Where Loads Many Resources

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?

FIRST SCRIPT

require('path/to/local/API_2');

//Check if user has put a query and that it's not empty
if (isset($_GET['query']) && !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&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 <= $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->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.='
                <div class="result">
                    <h3>'.$varlist[$i]['id'].'</h3>
                    <p>'.$varlist[$i]['INFO_1'].'</p>
                    <p>'.$varlist[$i]['INFO_2'].'</p>
                </div>';
        } 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();
}`

NOTE: $result equals HTML information from each time arround the loop.

NOTE: Almost all time is spent in the for ($i = 0; $i <= 7; ++$i) loop.

SECOND SCRIPT

//Same API as before 
require('path/to/local/API_2');

//Check if query is set and not empty
if (isset($_GET['query']) && !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->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->GET_FUNCTION_2($query['id']);
    $query['INFO_ADRESS_2.1'] = $ALL_INFO_3['INFO_ADRESS_3'];
    $ALL_INFO_3 = $Api->GET_FUNCTION_3($query['id']);
    $query['INFO_ADRESS_3.1'] = $ALL_INFO_4['INFO_ADRESS_4'];             
    $ALL_INFO_4 = $Api->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->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();
}`

NOTE: Similiarly to the first script, most time is spent getting info from the secondary API.

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'].

NOTE: Again, $result is the HTML data.

Upvotes: 1

Views: 2279

Answers (1)

Jonathan Kuhn
Jonathan Kuhn

Reputation: 15301

You could also move the API calls out from your normal page load. Respond to the user with a generic page to show something is happening and then make an ajax request to query the APIs and respond with data. There really is no way to speed up an individual external request. Your best bet is to:

  1. try to minimize the number of requests (even if it means you request a little more data once then filter out on your side vs sending multiple requests for a small subset of data).
  2. cache any remaining requests and pull from cache.
  3. respond with a small page to let the user know something is happening and make separate ajax requests for the queried data.

Upvotes: 1

Related Questions