您好,欢迎访问一九零五行业门户网

php中curl返回false该如何解决?(示例讲解)

本文介绍一下自己在使用curl中遇到的问题解决办法。希望可以帮助到大家。
首先来看一个封装的curl函数
function request_post($url = '', $param = '') { if (empty($url) || empty($param)) { return false; } $posturl = $url; $curlpost = $param; $curl = curl_init();//初始化curl curl_setopt($curl, curlopt_url,$posturl);//抓取指定网页 curl_setopt($curl, curlopt_header, 0);//设置header curl_setopt($curl, curlopt_returntransfer, 1);//要求结果为字符串且输出到屏幕上 curl_setopt($curl, curlopt_post, 1);//post提交方式 curl_setopt($curl, curlopt_postfields, $curlpost);//提交的参数 $data = curl_exec($curl);//运行curl curl_close($curl); return $data;}
调用的时候返回结果为bool(false)
我们在curl_exec函数前面通过curl_error($curl)获取错误也是 string(0) "" 空字符串。
最后发现自己调用的api的接口地址是ssl协议的,然后加上下面两个就可以了
curl_setopt($curl, curlopt_ssl_verifypeer, false);curl_setopt($curl, curlopt_ssl_verifyhost, false);
如果curl请求的地址中含有空格也会返回false的,这块也要格外注意
之前还遇到过一个返回false打印curl_error($curl)得到的是如下错误
string(39) "problem (2) in the chunked-encoded data" bool(false)
这个错误的解决办法设置curl使用的http协议版本,就是加上下面这句
//curl_http_version_1_0 (强制使用 http/1.0)//curl_http_version_1_1 (强制使用 http/1.1)。curl_setopt($curlp, curlopt_http_version, curl_http_version_1_1);
以上就是php中curl返回false该如何解决?(示例讲解)的详细内容。
其它类似信息

推荐信息