duanqinjiao5244 2014-08-06 16:40
浏览 66

如何在第一次调用脚本时修复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.

  • 写回答

1条回答 默认 最新

报告相同问题?

悬赏问题

  • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入
  • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计
  • ¥70 PlayWright在Java上连接CDP关联本地Chrome启动失败,貌似是Windows端口转发问题
  • ¥15 帮我写一个c++工程
  • ¥30 Eclipse官网打不开,官网首页进不去,显示无法访问此页面,求解决方法
  • ¥15 关于smbclient 库的使用
  • ¥15 微信小程序协议怎么写
  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启