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中flush()、ob_flush()、ob_end_flush()的区别介绍

flush()、ob_flush()、ob_end_flush()三者的区别:首先,说下buffer,它是一个内存地址空间,为4096(1kb)【在php.ini配置文件中找到outpu...

php str_pad 函数使用详解

string str_pad ( string , int pad_length , string pad_string , int pad_type); string 指定字符串,pa...

php 传值赋值与引用赋值的区别

传值赋值:当将一个表达式的值赋予一个变量时,整个原始表达式的值被赋予到目标变量。这意味着,例如,当一个变量的值赋予另一个变量时,改变其中一个变量的值,将不会影响到另一个变量。 复制代码...

PHP中集成PayPal标准支付的实现方法分享

PHP中集成PayPal标准支付的实现方法分享

PayPal支付功能其实一直在更新文档和接口,这里说的是一个简单的支付功能大概流程如下 1,在网站的结账页面,设置一个提交到PayPal网站的form,里面有一些金额,商品名称,商家收款...

PHP文件上传问题汇总(文件大小检测、大文件上传处理)

由于涉及到本地和服务器两方面的安全问题,所以基于input type="file"形式的页面文件上传一直处于一个很尴尬的位置。一方面,用户不希望隐私泄露,所以浏览器无法对用户在上传时选择...