如何在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
相关产品推荐
相关产品推荐

