如何在第一次调用脚本时修复cURL错误“SSL连接超时”?

I'm facing a strange problem using cURL with PHP on a Windows server. I have a very basic code:

private function curlConnection($method, $url, $timeout, $charset, array $data = null)
            {

                if (strtoupper($method) === 'POST') {
                    $postFields = ($data ? http_build_query($data, '', '&') : "");
                    $contentLength = "Content-length: " . strlen($postFields);
                    $methodOptions = array(
                        CURLOPT_POST => true,
                        CURLOPT_POSTFIELDS => $postFields,
                    );
                } else {
                    $contentLength = null;
                    $methodOptions = array(
                        CURLOPT_HTTPGET => true
                    );
                }


                $options = array(
                CURLOPT_HTTPHEADER => array(
                    "Content-Type: application/x-www-form-urlencoded; charset=" . $charset,
                    $contentLength,
                    'lib-description: php:' . PagSeguroLibrary::getVersion(),
                    'language-engine-description: php:' . PagSeguroLibrary::getPHPVersion()
                ),
                CURLOPT_URL => $url,
                CURLOPT_RETURNTRANSFER => true,
                CURLOPT_HEADER => true,
                CURLOPT_SSL_VERIFYPEER => false,
                CURLOPT_CONNECTTIMEOUT => $timeout
                );

                $options = ($options + $methodOptions);

                $curl = curl_init();
                curl_setopt_array($curl, $options);
                $resp = curl_exec($curl);
                $info = curl_getinfo($curl);
                $error = curl_errno($curl);
                $errorMessage = curl_error($curl);
                curl_close($curl);
                $this->setStatus((int) $info['http_code']);
                $this->setResponse((String) $resp);
                if ($error) {
                    throw new Exception("CURL can't connect: $errorMessage");
                } else {
                    return true;
                }
            }

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":

  1. Call the script -> SSL connection timeout
  2. Call the script again -> works fine
  3. Call the script one more time -> works fine
  4. Call the script n more times -> works fine
  5. Wait 10 minutes
  6. Call the script -> SSL connection timeout
  7. 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.

PHP - 5.2.17 CURL - libcurl/7.16.0 OpenSSL/0.9.8q zlib/1.2.3 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?

Thanks.

Try using the ca-bundle.crt bundle available here https://raw.githubusercontent.com/bagder/ca-bundle/master/ca-bundle.crt

Upload file

curl_setopt($ch, CURLOPT_CAINFO, "path");

Reference: http://richardwarrender.com/2007/05/the-secret-to-curl-in-php-on-windows

Hope this helps.