curl和ssl:curl没有数据通过https(GET或POST)

时间:2017-08-18 09:22:56

标签: php curl

我对curl和ssl有一些奇怪的问题。我在我的Ec2 AMZ AMI机器上有这个PHP脚本:

<?php
$ch = curl_init();
$url = 'https://rubenortiz.es/';
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, false);
curl_setopt($ch, CURLOPT_NOBODY, true);
curl_setopt($ch, CURLOPT_HEADER, 1);
curl_setopt($ch,CURLOPT_FAILONERROR,true);

$output = curl_exec($ch);
$info = curl_getinfo($ch);

if ($output === false || $info['http_code'] != 200) {
  $output .= "No cURL data returned for $url [". $info['http_code']. "]";
  if (curl_error($ch))
    $output .= "\n". curl_error($ch);
  }
else {
  // 'OK' status; format $output data if necessary here:
  $output .= 'OK';
}
print_r($output);
// then return or display the single string $output
?>

Error:
No cURL data returned for https

当我从EC2外部执行脚本 over Nginx PhP-FPM 时会发生这种情况,例如我的笔记本电脑。

但是如果我在 php-cli上运行相同的脚本就可以了!!!

[root@ ~]# php /home/webs//test.php
HTTP/1.1 200 OK
Server: nginx/1.13.3
Date: Fri, 18 Aug 2017 09:27:45 GMT
Content-Type: text/html; charset=UTF-8
Connection: keep-alive
X-Powered-By: PHP/5.6.31
Vary: Accept-Encoding, Cookie
Cache-Control: max-age=3, must-revalidate

总结

  • EC2 PHP-FPM curl - &gt;某些域名超过ssl = FAILS
  • EC2 PHP-FPM curl - &gt;某些域名非ssl = 我获取数据!
  • EC2 PHP-CLI curl - &gt;某些域名超过ssl = 我获取数据!
  • EC2 PHP-CLI curl - &gt;某些域名非ssl = 我获取数据!

    PHP 5.6.26(cli)(建于2016年10月6日19:48:12) 版权所有(c)1997-2016 PHP小组 Zend Engine v2.6.0,版权所有(c)1998-2016 Zend Technologies     Zend Technologies的Zend OPcache v7.0.6-dev,Copyright(c)1999-2016,

    OpenSSL 1.0.1g-fips 2014年4月7日

  • 卷曲7.51.0-6.74.amzn1.x86_64

  • python26-pycurl-7.19.0-17.12.amzn1.x86_64
  • 的libcurl-7.51.0-6.74.amzn1.x86_64
  • python27-pycurl-7.19.0-17.12.amzn1.x86_64

如果您查看$ url,您会看到这是一个https连接。如果我改变这个,我得到的数据没有问题。无论我测试什么网址,如果是https curl都无法给我信息。

有趣的是,我的另一台EC2上的这个剧本非常完美。我检查过很多东西,rpm包,php版本,fpm,nginx等等。

唯一不同的是在EC2机器上给我一个错误我运行了yum update命令,而且某些库有更新的版本。但这不是什么大问题。

所以重点是,出于某种原因,我无法通过SSL curl PHP-FPM从远程URL获取数据,听起来很棘手,我已经花了很多时间调试,但没有成功:S

有什么想法吗?

谢谢!

更新:

我最近运行了yum update来升级某个软件包,删除了软件包列表

Aug 14 07:07:47 Erased: python-boto-2.28.0-1.0.amzn1.noarch
Aug 14 07:07:47 Erased: python-requests-1.2.3-5.7.amzn1.noarch
Aug 14 07:07:47 Erased: python-urllib3-1.7-4.6.amzn1.noarch
Aug 14 07:07:47 Erased: python-backports-ssl_match_hostname-3.4.0.2-1.5.amzn1.noarch
Aug 14 07:07:48 Erased: python-paramiko-1.7.5-2.1.4.amzn1.noarch
Aug 14 07:07:48 Erased: python-crypto-2.6.1-1.7.amzn1.x86_64
Aug 14 07:07:48 Erased: newt-python-0.52.11-3.7.amzn1.x86_64
Aug 14 07:07:48 Erased: python-simplejson-3.3.0-1.5.amzn1.x86_64
Aug 14 07:07:48 Erased: python-backports-1.0-3.2.amzn1.x86_64
Aug 14 07:07:48 Erased: pystache-0.5.3-2.3.amzn1.noarch
Aug 14 07:07:48 Erased: python-ordereddict-1.1-2.2.amzn1.noarch
Aug 14 07:07:48 Erased: python-six-1.2.0-1.3.amzn1.noarch
Aug 14 07:07:48 Erased: python-chardet-2.0.1-1.2.amzn1.noarch
Aug 14 07:07:48 Erased: python-argparse-1.2.1-2.2.amzn1.noarch
Aug 14 07:07:48 Erased: python-rsa-3.1.2-4.3.amzn1.noarch
Aug 14 07:07:48 Erased: python-setuptools-0.6.10-3.11.amzn1.noarch
Aug 14 07:07:48 Erased: python-urlgrabber-3.9.1-9.10.amzn1.noarch
Aug 14 07:07:48 Erased: python-iniparse-0.3.1-2.1.7.amzn1.noarch
Aug 14 07:07:48 Erased: rpm-python-4.11.2-2.54.amzn1.x86_64
Aug 14 07:07:49 Erased: python-pycurl-7.19.0-8.7.amzn1.x86_64
Aug 14 07:07:49 Erased: pyxattr-0.5.0-1.4.amzn1.x86_64
Aug 14 07:07:49 Erased: pygpgme-0.1-18.20090824bzr68.8.amzn1.x86_64
Aug 14 07:07:49 Erased: pyliblzma-0.5.3-3.6.amzn1.x86_64
Aug 14 07:07:49 Erased: 1:python-2.6-2.26.amzn1.noarch

也许有人在这里看到一些线索?

2 个答案:

答案 0 :(得分:0)

当网址为CURLOPT_SSL_VERIFYPEER

时,您必须将false添加到https
$ch = curl_init();
$url = 'https://rubenortiz.es/';
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, false);
curl_setopt($ch, CURLOPT_NOBODY, true);
curl_setopt($ch, CURLOPT_HEADER, 1);
curl_setopt($ch,CURLOPT_FAILONERROR,true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); //<----Add this line

$output = curl_exec($ch);
$info = curl_getinfo($ch);

if ($output === false || $info['http_code'] != 200) {
  $output .= "No cURL data returned for $url [". $info['http_code']. "]";
  if (curl_error($ch))
    $output .= "\n". curl_error($ch);
  }
else {
  // 'OK' status; format $output data if necessary here:
  $output .= 'OK';
}
print_r($output);
?>

注意:建议使用ssl证书并在curl中包含以防止数据

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

答案 1 :(得分:-1)

非常感谢您对此问题的评论和努力。我实际上可以回答我的问题。问题有点棘手。我前几天跑了yum更新更新第三个包,比如&#34; s3cmd&#34;。好吧,我不知道发生了什么,然后我确定它破坏了什么。然后我想试一试再次运行yum update。瞧!

现在相同的脚本正在运行。

再次感谢您的帮助!

此致

[R