You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

创建REST API时遇SSL证书错误:无法获取本地颁发者证书

解决cURL SSL证书错误:unable to get local issuer certificate

针对你在WAMP环境中调用HTTPS接口时遇到的SSL证书问题,以下是具体解决方案:

一、全局配置PHP证书路径(推荐)

  1. 打开WAMP的PHP配置文件:点击WAMP托盘图标 → PHP → php.ini
  2. 在文件中找到curl.cainfo和openssl.cafile两项,取消注释并设置为你放置的cacert.pem绝对路径,示例:
    curl.cainfo = "C:/wamp64/www/cacert.pem"
    openssl.cafile = "C:/wamp64/www/cacert.pem"
    
  3. 重启WAMP服务,使配置生效

二、在代码中直接指定证书路径

如果不想修改全局配置,可以在cURL选项中添加证书路径设置,修改后的代码如下:

<?php

// 设置请求URL和请求头
$url = 'https://www.axxess.co.za/careers/dev-test';
$headers = array(
    'Authorization: Basic ' . base64_encode('applicant:canCode'),
    'Content-Type: application/json'
);

// 设置请求体字段和值
$data = array(
    'first_name' => 'James',
    'last_name' => 'Nelson',
    'mobile_number' => '0739950802',
    'email_address' => 'jackson@gmail.com'
);
$data_string = json_encode($data);

// 初始化cURL会话
$ch = curl_init();

// 设置cURL选项
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
// 启用证书验证并指定本地证书文件
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true);
curl_setopt($ch, CURLOPT_CAINFO, __DIR__ . '/cacert.pem');

// 执行请求并获取响应
$response = curl_exec($ch);

// 检查错误
if(curl_errno($ch)) {
    echo '错误: ' . curl_error($ch);
}

// 关闭cURL会话
curl_close($ch);

// 输出响应
echo $response;
?>

三、排查常见问题

  • 确保cacert.pem是最新的根证书集合,避免因证书过期导致验证失败
  • 检查文件权限,保证WAMP的运行用户能读取该证书文件
  • 若临时需要禁用证书验证(不推荐用于生产环境),需同时设置CURLOPT_SSL_VERIFYHOST=0,但这会降低请求安全性

内容的提问来源于stack exchange,提问作者MyLegend2020 Wilson

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.25 15:15:11