How to set a timeout for a cURL request?

To set a timeout for a cURL request, you can use the CURLOPT_TIMEOUT option. Here's an example of how you can set a timeout of 10 seconds:

$ch = curl_init(); // Set the URL of the request curl_setopt($ch, CURLOPT_URL, "http://example.com"); // Set the timeout in seconds curl_setopt($ch, CURLOPT_TIMEOUT, 10); // Execute the request $response = curl_exec($ch); // Check for errors if(curl_errno($ch)){ echo 'cURL error: ' . curl_error($ch); } // Close the cURL handle curl_close($ch);

In this example, CURLOPT_TIMEOUT is set to 10, which means the cURL request will timeout if it takes longer than 10 seconds to complete. You can adjust the timeout value as per your requirement.