Skip to content
PHP Logo

In PHP, what are some ways to log errors and continue script execution when a server does not respond to a request made with cURL?

When a server does not respond to a request made with cURL in PHP, you can log errors and continue script execution by setting a timeout option for the cURL request. This allows you to define a maximum time for the request to wait for a response before timing out and moving on with the script execution.

php
// Initialize cURL session
$ch = curl_init();

// Set cURL options
curl_setopt($ch, CURLOPT_URL, 'http://example.com/api');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_TIMEOUT, 10); // Set timeout to 10 seconds

// Execute cURL request
$response = curl_exec($ch);

// Check for cURL errors
if(curl_errno($ch)) {
    $error_message = curl_error($ch);
    // Log the error message
    error_log('cURL error: ' . $error_message);
}

// Close cURL session
curl_close($ch);

// Continue script execution
// Process $response data or handle error as needed