我在Windows服务器上使用cURL和PHP时遇到了一个奇怪的问题。我有一个非常基本的代码:
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;
}
}
问题是第一次调用这个脚本时,响应总是这样:"SSL连接超时".
对脚本的后续调用输出所需的结果,但是,如果在再次调用脚本之前等待几分钟,则超时问题再次发生。
那么,复制"error"的步骤:
- 调用脚本-> SSL连接超时
- 再次调用脚本->工作正常
- 再次调用脚本->工作正常
- 调用脚本n次->工作正常
- 等待10分钟
- 调用脚本-> SSL连接超时
- 再次调用n次脚本->工作正常
如果我调用任何其他脚本,响应是立即的,即使经过一段时间的不活动,所以这种行为只发生在涉及cURL时。
PHP - 5.2.17libcurl/7.16.0 OpenSSL/0.9.8q zlib/1.2.3服务器运行Windows 2012和IIS 8,最新升级,在FastCGI上运行PHP。
有谁知道我该怎么解决这个问题吗?
谢谢。
尝试使用ca-bundle。CRT包可在此下载https://raw.githubusercontent.com/bagder/ca-bundle/master/ca-bundle.crt
上传文件curl_setopt($ch, CURLOPT_CAINFO, "path");
参考:http://richardwarrender.com/2007/05/the-secret-to-curl-in-php-on-windows
尝试检查'Server' Windows服务的状态,如果停止-可能是原因。我不知道它是如何相关的,但它帮助我解决了同样的问题。