Current Location: Home> Latest Articles> How to mock curl_upkeep() call fails for testing

How to mock curl_upkeep() call fails for testing

M66 2025-06-02

In PHP, the curl_upkeep function is used to handle the maintenance and management of HTTP requests. Common usage scenarios include regular updates of data, checking service status, or load balancing. However, it is very important for developers to simulate the failure of curl_upkeep function call, especially during the testing phase, where it is necessary to verify how the system deals with different types of errors.

In this article, we will introduce several common methods to simulate a scenario where curl_upkeep call fails.

1. Simulated DNS resolution failed

A common failure is DNS resolution failure. Assuming that the domain name you request cannot be resolved to a valid IP address, you can trigger such an error by modifying the domain name in the URL.

 <?php
// simulation DNS Analysis failed
$url = "http://invalid-domain.m66.net"; // This domain name cannot be parsed
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);

if(curl_errno($ch)) {
    echo 'Error:' . curl_error($ch); // Output error message
}

curl_close($ch);
?>

In this example, invalid-domain.m66.net is an invalid domain name, causing the curl_exec function to fail to parse the URL correctly, thus returning a DNS error.

2. Simulated connection timeout

Another common failure situation is connection timeout. If the server does not respond within the specified time, curl reports a connection timeout error. You can simulate this by setting a smaller timeout.

 <?php
// simulation连接超时
$url = "http://m66.net"; // Valid domain name
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_TIMEOUT, 1); // Set the timeout time to1Second

$response = curl_exec($ch);

if(curl_errno($ch)) {
    echo 'Error:' . curl_error($ch); // Output error message
}

curl_close($ch);
?>

In this example, since the timeout is set very short, if the server does not respond within 1 second, curl_exec will return a timeout error.

3. Simulate server error (5xx)

Another failure situation is an error occurred in the server (such as 500, 502, etc.). This situation is usually caused by server-side problems, such as code errors, resource exhaustion, or internal failures. You can simulate this by requesting a URL that is deliberately configured incorrectly.

 <?php
// simulation服务器mistake (5xx)
$url = "http://m66.net/server-error"; // Assume that the path returns 500 mistake
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

$response = curl_exec($ch);

if(curl_errno($ch)) {
    echo 'Error:' . curl_error($ch); // Output error message
} else {
    $http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
    if ($http_code >= 500 && $http_code < 600) {
        echo "服务器mistake,HTTP Status code: $http_code"; // simulation 5xx mistake
    }
}

curl_close($ch);
?>

In this example, server-error assumes that it returns a 500 error, emulating the server error.

4. Simulate network disconnection

If you need to simulate network problems, such as network interruptions, you can simulate by disabling the network connection or using a local server. In some development environments, you can disconnect the network or simulate a network interrupt to trigger similar errors.

5. Simulation is unresponsive

You can also simulate a situation where the server is completely unresponsive. For example, use an unresponsive URL, or locally emulate a server that cannot return content.

 <?php
// simulation服务器无响应
$url = "http://m66.net/no-response"; // Assume that the path is unresponsive
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_TIMEOUT, 5); // Set the timeout time to5Second

$response = curl_exec($ch);

if(curl_errno($ch)) {
    echo 'Error:' . curl_error($ch); // Output error message
}

curl_close($ch);
?>

Here, no-response is a hypothetical path that simulates an unresponsive situation.

6. Simulation request denied (4xx)

Common 4xx errors, such as 403 (Forbidden), 404 (Not Found), etc., can be simulated by requesting a resource that does not exist or does not have access rights.