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

如何在Guzzle中传递多证书实现Curl请求转换?

解决Guzzle中配置密钥、证书及证书链的问题

你的curl请求用到的三个证书相关参数,对应Guzzle的配置项如下:

  • --key key.pem → ssl_key:指定客户端私钥文件路径
  • --cert cert.pem → cert:指定客户端证书文件路径
  • --cacert client-bundle.pem → verify:指定CA证书链文件路径

把这些配置添加到你的Guzzle请求数组即可,修改后的完整代码如下:

try {
    $response = $this->request('POST', 'my_url', [
        'headers' => [
            'Content-Type' => 'application/x-www-form-urlencoded',
            'Accept' => 'application/json'
        ],
        // 新增证书相关配置
        'ssl_key' => 'key.pem', // 对应curl的--key参数
        'cert' => 'cert.pem',   // 对应curl的--cert参数
        'verify' => 'client-bundle.pem' // 对应curl的--cacert参数
    ]);

    $result = Json::decode($response->getBody()->getContents());
    $this->checkResponse($result); // 原代码中该行在return之后无法执行,已调整顺序
    return $result;

} catch (GuzzleException $e) {
    return $e->getMessage();
}

额外说明

  • 建议使用证书文件的绝对路径,避免因相对路径导致文件找不到的问题
  • 如果证书设置了密码,可将cert和ssl_key配置为数组形式,例如:'cert' => ['cert.pem', 'your_password']、'ssl_key' => ['key.pem', 'your_password']

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.24 19:12:52