How to resolve long http request timeout

Abhigyan Singh 19th Jul 2020

We stuck in some situation where we encounter with the HTTP request timeout problem. This kind of HTTP request timing out may happen due to very heavy and complex manipulation of code and queries or have receive high traffic, poorly coded script or have some host issue.

This long HTTP request badly impact website SEO ranking because google only love those site which is able to deliver all the HTTP request within the specific time frame. Here we will discuss few of the best techniques on which we can resolve such issue from our website. We have also explained to resolve connection timeout on google chrome in one of my previous article.

Steps to resolve long HTTP request timeout

1. 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

// 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

// This is the below query
echo "This is below script from the complex script \n";
echo file_get_contents('url');


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.

2. 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
$head = curl_exec($ch);
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);

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(){
success: function(){
function secondAjaxrequest(){
success: function(){
function thirdAjaxrequest(){
success: function(){

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.

5. 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

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

  1. Locate the php.ini file as per your system configuration.
  2. Open the file and find max_execution_time
  3. change the value of this directive
  4. 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

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.

Now you can use the above listed technique and let us know which one is your favorite. If you have any difficulties, feel free to write to us in comment section.

Authored By Abhigyan Singh

He is a continuous blogger and has blogged on different topic. He loves to surf Internet and always trying to get new Idea about new Technology and Innovations and sharing these great information to all the technology lovers.