欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

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

程序员文章站 2022-04-28 15:48:07
症状:php curl调用https出错 排查方法:在命令行中使用curl调用试试。 原因:服务器所在机房无法验证ssl证书。 解决办法:跳过ssl证书检查。 curl_s...
症状: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.