本文主要是介绍PHP CURL HTTPS报错SSL certificate problem: unable to get local issuer certificate,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
起因:本地调用java接口返回无数据,但测试环境却是有数据的。
调试过程如下:
1,curl_getinfo — 获取一个cURL连接资源句柄的信息。
Array
([url] => https://grapserver.linkstars.com/goods/getSearch[content_type] => [http_code] => 0[header_size] => 0[request_size] => 0[filetime] => -1 //重点 远程获取文档的时间,如果无法获取,则返回值为“-1”[ssl_verify_result] => 20[redirect_count] => 0[total_time] => 0.097583[namelookup_time] => 0.000464[connect_time] => 0.044156[pretransfer_time] => 0[size_upload] => 0[size_download] => 0[speed_download] => 0[speed_upload] => 0[download_content_length] => -1[upload_content_length] => -1[starttransfer_time] => 0[redirect_time] => 0[redirect_url] => [primary_ip] => 62.234.91.232[certinfo] => Array()[primary_port] => 443[local_ip] => 10.254.30.29[local_port] => 61604[http_version] => 0[protocol] => 2[ssl_verifyresult] => 0[scheme] => HTTPS[appconnect_time_us] => 0[connect_time_us] => 44156[namelookup_time_us] => 464[pretransfer_time_us] => 0[redirect_time_us] => 0[starttransfer_time_us] => 0[total_time_us] => 97583
)
2,curl_error — 返回当前会话最后一次错误的字符串
SSL certificate problem: unable to get local issuer certificate
解决方法1
如果请求HTTPS的时候,默认会上述报错:
众所周知HTTPS需要证书认证,如果本地没有装,就会报这个错误,可以把ssl认证选项在curl发送请求时关掉。
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('X-AjaxPro-Method:ShowList','Content-Type: application/json; charset=utf-8','Content-Length: ' . strlen($data_string))
);
// 关闭SSL验证
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
if ($data_string) {// 设置请求方式为postcurl_setopt($ch, CURLOPT_POST, 1);// post的变量curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
}else{// 设置请求方式为postcurl_setopt($ch, CURLOPT_POST, 0);
}
$data = curl_exec($ch);
$info = curl_getinfo($ch);
print_r($info);
curl_close($ch);
解决方法2
从curl官方网站,下载根证书:cacert.pem ,然后修改php.ini中加入该证书,并重启web服务。
#在php.ini中加入 [SSL]
curl.cainfo ="D:\phpstudy_pro\Extensions\php\php7.3.4nts\extras\ssl\cacert.pem"
openssl.cafile = "${curl.cainfo}"
这篇关于PHP CURL HTTPS报错SSL certificate problem: unable to get local issuer certificate的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!