I'm facing a strange problem using cURL with PHP on a Windows server. I have a very basic code that I'm using to test this issue (pretty much straight from the php cURL manual page):
<?php
// In this example we are referring to a page that handles html
$headers = array( "Content-Type: text/html");
// Initialise Curl
$curl = curl_init();
if ($curl === false)
throw new Exception(' cURL init failed');
// Configure curl for website
curl_setopt($curl, CURLOPT_URL, "https://google.com");
// Set up to view correct page type
curl_setopt($curl, CURLOPT_HTTPHEADER, &$headers);
// Turn on SSL certificate verfication
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 1);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, TRUE);
// Tell the curl instance to talk to the server using HTTP GET
curl_setopt($curl, CURLOPT_HTTPGET, 1);
// 1 second for a connection timeout with curl
curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, 5);
// Try using this instead of the php set_time_limit function call
curl_setopt($curl, CURLOPT_TIMEOUT, 60);
// Causes curl to return the result on success which should help us avoid using the writeback option
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
if( ! $result = curl_exec($curl))
$result = curl_error($curl);
curl_close($curl);
echo var_dump($result);
?>
Sorry for the code dump, but it's pretty basic code and all it does is try to GET the google.com site using HTTPS (SSL).
The problem is that the first time this script is called, the response is always this: string(22) "SSL connection timeout".
Subsequent calls to the script output the desired result, but, if I wait a couple of minutes before calling the script again, the timeout issue happens again.
So, steps to reproduce the "error":
- Call the script -> timeouts
- Call the script again -> works fine
- Call the script one more time -> works fine
- Call the script n more times -> works fine
- Wait 10 minutes
- Call the script -> timeouts
- Call the script n more times again -> works fine
If I call any other script the response is immediate, even after a period of inactivity, so this behaviour only happen when cURL is involved.
All components are running the latest stable version, including PHP, cURL, OpenSSL, etc. The server is running Windows 2012 with IIS 8, latest upgrades, running PHP on FastCGI.
Does anyone have any idea on how I can solve this? Currently I have a cron job running every 5 minutes and calling the script above to refresh cURL, but that feels like a hack :(
Thanks.