PHP CURL HTTPS报错SSL certificate problem: unable to get local issuer certificate

本地使用PHP的cURL调用Java接口时,遇到HTTPS报错:SSL certificate problem: unable to get local issuer certificate。通过curl_getinfo和curl_error进行调试。解决方案包括禁用SSL验证或下载根证书并配置到php.ini中。

起因:本地调用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) {
    // 设置请求方式为post
    curl_setopt($ch, CURLOPT_POST, 1);
    // post的变量
    curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);     
}else{
    // 设置请求方式为post
    curl_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}"

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值