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.

相关文章

手把手教你打印出PDF(关于fpdf的简单应用)

今天使用的类叫FPDF,FPDF这个PHP Class允许你采用纯PHP(更确切地说就是不需要使用PDFlib)来生成PDF文件。它以PHP Class展现并且加速PDF文档在编程语言中...

基于PHP导出Excel的小经验 完美解决乱码问题

我在PHP项目里要求把数据导出为Excel,并且数据中包含中文.网上大概了解一下可是使用PHPExcel,可是相对我的需求,这个框架太复杂了.于是还是想找找简单做法.网上发现其实最简单可...

PHP 优化配置——加速你的VBB,phpwind,Discuz,IPB,MolyX第1/2页

让论坛速度更快 PHP加速设置 PHP加速:Zend Optimizer优化PHP程序 Zend Optimizer V2.5.7&nb...

如何使用PHP往windows中添加用户

方法有一:   因为添加用户,所以你运行PHP程序的用户必须是管理员权限(Administrator),并且同时需要你的php.ini中的安全模式没有打开,并且关闭函...

PHP 加密解密内部算法

将它们打包成一个文件就叫fun.php吧 复制代码 代码如下: <?php function passport_encrypt($txt, $key) { srand((doubl...