Steps to resolve long HTTP request timeout
- Sleep Function
The sleep function is an inbuilt function of PHP. It just make delay the execution of current script by specifying time in seconds. The sleep() function accepts second as parameter and give binary true/false as output.
Syntax of Sleep() function
<?php
// It just print the below line.
echo "This is above script from the complex script \n";
// It just sleep or don't resolve this request for 10 seconds
sleep(10);
// This is the below query
echo "This is below script from the complex script \n";
echo file_get_contents('url');
?>
PHP
The above script delay the execution of HTTP request by 10 second. It means that it print the first echo message and then sleep for 10 second and then again print the below message.
- cURL
As we know cURL is a library which is used to make HTTP request in PHP and transferring the data in url syntax. cURL is used to make request from one server to another server and handle or transfer bulk amount of data.So, we need to enable cURL library in order to use this function. you need to install and enable cURL in you web sever before using it.
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER, TRUE);
curl_setopt($ch, CURLOPT_NOBODY, TRUE); // remove body
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
$head = curl_exec($ch);
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
PHP
3. Ajax or multi Ajax request
AJAX stands for Asynchronous JavaScript And XML. So the name itself describe that it works Asynchronously and handle Asynchronous request. The Asynchronous request means that the request serves one after another. No simultaneous or multiple request will handle same time.
Now we can divide our script into multiple chunks and call it through the AJAX request. So the large HTTP request is now divided into multiple small request and each AJAX request consider as single HTTP request. Now the server can this request and can able to resolve request timeout situation.
function firstAjaxrequest(){
$.ajax({
success: function(){
secondAjaxrequest();
}
});
}
function secondAjaxrequest(){
$.ajax({
success: function(){
thirdAjaxrequest();
}
});
}
function thirdAjaxrequest(){
$.ajax({
success: function(){
fourAjaxrequest();
}
});
}
JavaScript
4. Close, open and close database connectivity
Recently, I have used this technique in one of my application. Actually, I was in need to filter the data from our database based on several condition and pass those values into one of the third party API and get the result back to our application based on that input values and store those response into our database.
Here our query was also very complex to get the filtered data and the third party API is very slow to give the response. So in this case, every time my script give request timeout issue.
My solution : I just close the connection of database just above the call of third party api because on the above, i have written script to get the filtered data from our script. Once the third party API called and get the response then again I opened the database connection and store those value into our database. So I achieve to resolve this HTTP request timeout issue by using this technique.
- increase the max execution time of PHP script
This is very quick fix solution for HTTP request timeout problem or handle the heavy script code. The max execution time of PHP script can be increased by two way:
(a). Add the below line in the beginning of the script where the heavy execution is happening. This will be only applicable for that page where you have used this code.
Note : This code must use from very beginning of the script.
ini_set(‘max_execution_time’, 120); // 120 (seconds) = 2 Minutes
ini_set(‘max_execution_time’, 180); // 180 (seconds) = 3 Minutes
ini_set(‘max_execution_time’, 0); // 0 = Unlimited times
PHP
Setting the max_execution_time as ‘0’ is not a good practice on live server. So I recommend it to avoid in live server.
(b). Setting the Max_Execution_Time globally in php.ini file.
The below step will show you to change the max_execution_time in php.ini file
Locate the php.ini file as per your system configuration.
Open the file and find max_execution_time
change the value of this directive
Restart your Apache web server
; Maximum execution time of each script, in seconds
; Note: This directive is hardcoded to 0 for the CLI SAPI
max_execution_time = 30
PHP
Here also setting the value as ‘0’, gives the unlimited execution time of your php script so this is not recommended.
This change will be applicable for your entire application as you have done the changes in php configuration file.
I have tested all the above listed technique to resolve the request timeout problem but the sleep() method and Closing and opening database connectivity is my favorite one.
Regards,
Rachel Gomez