【问题标题】:How to fix cURL error "SSL connection timeout" only on the first time the script is called?如何仅在第一次调用脚本时修复 cURL 错误“SSL 连接超时”?
【发布时间】: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"。

对脚本的后续调用会输出所需的结果,但是,如果我在再次调用脚本之前等待几分钟,则会再次发生超时问题。

所以,重现“错误”的步骤:

  1. 调用脚本->SSL连接超时
  2. 再次调用脚本 -> 工作正常
  3. 再次调用脚本 -> 工作正常
  4. 再调用脚本 n 次 -> 工作正常
  5. 等待 10 分钟
  6. 调用脚本 -> SSL 连接超时
  7. 再次调用脚本 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。

有人知道我该如何解决这个问题吗?

谢谢。

【问题讨论】:

    标签: php windows curl


    【解决方案1】:

    尝试使用此处提供的 ca-bundle.crt 包 https://raw.githubusercontent.com/bagder/ca-bundle/master/ca-bundle.crt

    上传文件

    curl_setopt($ch, CURLOPT_CAINFO, "路径");

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

    希望这会有所帮助。

    【讨论】:

      【解决方案2】:

      尝试检查“服务器”Windows 服务的状态,如果停止 - 可能是原因。不知道它是如何相关的,但它对我解决同样的问题有所帮助。

      【讨论】:

      • 正如目前所写,您的答案尚不清楚。请edit 添加其他详细信息,以帮助其他人了解这如何解决所提出的问题。你可以找到更多关于如何写好答案的信息in the help center
      猜你喜欢
      • 2019-06-25
      • 2020-05-04
      • 2015-08-20
      • 2015-02-11
      • 2016-07-11
      • 2012-02-25
      • 2013-08-18
      • 2019-11-01
      • 2019-11-01
      相关资源
      最近更新 更多