php curl常见错误:SSL错误、bool(false)

yipeiwu_com5年前PHP代码库
症状:php curl调用https出错
排查方法:在命令行中使用curl调用试试。
原因:服务器所在机房无法验证SSL证书。
解决办法:跳过SSL证书检查。
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);

症状:php curl调用curl_exec返回bool(false),命令行curl调用正常。
排查方法:
var_dump(curl_error($ch));
返回:
string(23) "Empty reply from server"
再排查:
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, false);
返回:
HTTP/1.1 100 Continue
Connection: close
原因:php curl接收到HTTP 100就结束了,应该继续接收HTTP 200
解决方案:
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Expect:'));

PHP and cURL: Disabling 100-continue header
Published June 15th, 2006
I've been using cURL (through PHP) to build a sort of proxy for a project I'm working on. I need to parse the returned headers (to recover the HTTP status), so had included a very simple script to do so. It had worked fine in the past, but for some reason barfed in this case. A closer look at what was being returned revealed that for some reason, Apache was prepending the ‘normal' headers with an extra response header:

HTTP/1.1 100 Continue

HTTP/1.1 200 OK Date: Fri, 09 Jun 2006 15:23:42 GMT
Server: Apache
...A bit of Googling revealed that this was to do with a header that cURL sends by default:

Expect: 100-continue

…which in turns tells Apache to send the extra header. I poked around a fair bit but couldn't quite find a workable solution short of manually removing the header in PHP, which seemed a bit clumsy. Finally, on a hunch I tried this:

curl_setopt( $curl_handle, CURLOPT_HTTPHEADER, array( 'Expect:' ) );

…which basically overrides the original ‘Expect:' header with an empty one.

Hope this helps someone.

相关文章

PHP实现事件机制实例分析

本文实例讲述了PHP实现事件机制的方法。分享给大家供大家参考。具体分析如下: 内置了事件机制的语言不多,php也没有提供这样的功能。事件(Event)说简单了就是一个Observer模式...

PHP里面把16进制的图片数据显示在html的img标签上(实现方法)

客户公司的SQLServer2008的一个生产数据库才1年多就高达18G之巨,原来是系统里面的图片直接以16进制字符串的形式存储在数据库的。要用PHP显示在html页面上,我还耗费了不上...

PHP中防止SQL注入方法详解

问题描述:   如果用户输入的数据在未经处理的情况下插入到一条SQL查询语句,那么应用将很可能遭受到SQL注入攻击,正如下面的例子: 复制代码 代码如下: $unsafe_variabl...

Thinkphp中import的几个用法详细介绍

下面附上import的几个用法介绍 1、用法一 import('@.Test.Translate'); @,表示项目根目录。假定根目录是:App/ 导入类库的路径是:App/Lib/Te...

汇总PHPmailer群发Gmail的常见问题

大家在PHPmailer群发Gmail时会遇到许多常见问题,下面为大家总结了一些常见问题,希望对大家的学习有所帮助。 1.Could not authenticate 首先,如果你没有使...