【发布时间】:2014-08-06 16:40:07
【问题描述】:
我在 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;
}
}
问题是第一次调用这个脚本时,响应总是这样:string(22) "SSL connection timeout"。
对脚本的后续调用会输出所需的结果,但是,如果我在再次调用脚本之前等待几分钟,则会再次发生超时问题。
所以,重现“错误”的步骤:
- 调用脚本->SSL连接超时
- 再次调用脚本 -> 工作正常
- 再次调用脚本 -> 工作正常
- 再调用脚本 n 次 -> 工作正常
- 等待 10 分钟
- 调用脚本 -> SSL 连接超时
- 再次调用脚本 n 次 -> 工作正常
如果我调用任何其他脚本,即使在一段时间不活动之后也会立即响应,因此这种行为仅在涉及 cURL 时才会发生。
PHP - 5.2.17 CURL - libcurl/7.16.0 OpenSSL/0.9.8q zlib/1.2.3 服务器正在运行带有 IIS 8 的 Windows 2012,最新升级,在 FastCGI 上运行 PHP。
有人知道我该如何解决这个问题吗?
谢谢。
【问题讨论】: