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官网定义:复制代码 代码如下:构造函数是类中的一个特殊函数,当使用 new 操作符创建一个类的实例时,构造函数将会自动调用。当函数与类同名时,这个函数将成为构造函数。如果一个类没有...

php下安装配置fckeditor编辑器的方法

一、PHP调用fckeditor方法。 二、JS调用fckeditor方法。 复制代码 代码如下: <?php require_once(PATH_PRE.”fckeditor.p...

PHP中应该避免使用同名变量(拆分临时变量)

当一个临时变量被赋值多次时,那么将其拆分成多个,除非它是一个循环计数器。 Motivation 临时变量有这多种不同的用途。比如它们可被用作循环中的计数器,在循环中保存结果集,亦或保存一...

DedeCMS dede_channeltype表字段注释

这是我花了一些时间整理出来的,但是还是有几个字段不清楚,希望知道的朋友们,告诉我! Dede_channeltype id 频道ID nid 识别ID typename 模型名字 mai...

php仿ZOL分页类代码

php仿ZOL分页类代码

复制代码 代码如下:<?php /** * 功能:分页类 * 作者:phpox * 时间:Sat Jul 14 18:15:02 CST 2007 */ defined('PH...