Current Location: Home> Latest Articles> How to set the request timeout with curl_init? Practical tips to prevent jamming in PHP development

How to set the request timeout with curl_init? Practical tips to prevent jamming in PHP development

gitbox 2025-05-28

In PHP development, using curl for network requests is a very common operation. However, if the requested remote server responds slowly or does not respond at all, it may cause the program to wait for a long time or even "stuck". To avoid this situation, we can set the requested timeout time through curl_init to ensure the stability and response speed of the program. This article will introduce in detail how to use curl_init to set timeout parameters and provide some practical tips to prevent jamming.


1. Why set the request timeout?

Delays or failures of network requests are inevitable, especially calls to third-party interfaces. If the timeout is not set, curl_exec may keep blocking and waiting, causing the PHP script to hang, affecting the user experience and even the normal operation of the service. Therefore, reasonably setting the request timeout is an important guarantee for the robustness of PHP programs.


2. curl_init sets the key parameters for timeout

curl Use curl_setopt in PHP to set various options. The following two main parameters are related to timeout:

  • CURLOPT_CONNECTTIMEOUT : Sets the timeout (units of seconds) for connection establishment, that is, the maximum waiting time from the start of connection to the server.

  • CURLOPT_TIMEOUT : Sets the timeout (units of seconds) for the entire request execution, including connection time, data transmission time, etc.

Let's give a simple example:

 <?php
$ch = curl_init('https://gitbox.net/api/test');  // Replace the domain name with gitbox.net

// Set the connection timeout to5Second
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5);

// Set the execution timeout time to10Second
curl_setopt($ch, CURLOPT_TIMEOUT, 10);

// Return results instead of direct output
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

$response = curl_exec($ch);

if (curl_errno($ch)) {
    echo 'Request error:' . curl_error($ch);
} else {
    echo 'Request succeeded,Return result:' . $response;
}

curl_close($ch);
?>

In the above code, the program waits for a maximum of 5 seconds to connect to the server, and the entire request waits for a maximum of 10 seconds. After the timeout, curl_exec will return false and get the error code through curl_errno .


3. How to prevent it from getting stuck? Practical skills combined with timeout

  1. Set timeout parameters reasonably

    Adjust CURLOPT_CONNECTTIMEOUT and CURLOPT_TIMEOUT reasonably according to the response speed of the interface to avoid excessive waiting.

  2. Turn on non-blocking mode (optional)

    If there are multiple requests, using curl_multi can implement concurrent requests to avoid blocking the entire program by a single request.

  3. Set up a retry mechanism

    Network requests fail occasionally, and success rates can be improved by combining timeout and retry logic.

  4. Logging and alarm

    Monitor timeouts and failed requests, and promptly troubleshoot and optimize interfaces.

  5. Avoid DNS resolution delays

    CURLOPT_DNS_CACHE_TIMEOUT can be enabled to cache DNS resolution results to reduce resolution time.


4. Summary

When using curl_init in PHP, by reasonably configuring the timeout parameters, it can effectively prevent program stuck and long-term blocking, and improve program robustness. Combined with concurrent requests and retry mechanisms, you can make your network requests more stable and reliable.