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

yipeiwu_com6年前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可逆加密/解密函数分享

函数源码 复制代码 代码如下: function encrypt($data, $key) { $prep_code = serialize($data); $block = mcryp...

php/JS实现的生成随机密码(验证码)功能示例

本文实例讲述了php/JS实现的生成随机密码(验证码)功能。分享给大家供大家参考,具体如下: PHP写法: //A-Z a-z 0-9 !#$%^&* $str = "ABCDEFG...

php模板中出现空行解决方法

在本地也解决了,但是上传到服务器依然有空行,搞了一个上午,都快崩溃了,最后决定自己想办法解决,经过几个小时的摸索终于有了完美解决方法了。 运用php显示缓冲区显示原理,成功去掉 在php...

.htaccess文件保护实例讲解

比如说,想要保护admin文件夹,经过以下两个步骤: 步骤一、可以用记事本新建文件.htaccess,输入以下内容: AuthType BasicAuth UserFile D:/App...

PHP利用APC模块实现大文件上传进度条的方法

php 大文件带进度的上传,一直是一个令php程序员很苦恼的问题。查询baidu 、Google ,大体做带进度的上传方式为:flash+php,socket,apc+php等,下面我介...