我有一个使用密钥、证书和证书链的 http 请求。如何翻译成 Guzzle?问题是我不知道如何将我所有的证书添加到 Guzzle 请求中。在文档中有一个只有一个证书的例子
我的要求示例:
// curl -vvv -X POST --header 'Content-Type: application/x-www-form-urlencoded' --header 'Accept: application/json' --cacert client-bundle.pem --key key.pem --cert cert.pem 'my_url'
我的功能目前正在使用 Guzzle 发送请求:
try {
$response = $this->request('POST', 'my_url', [
'headers' => [
'Content-Type' => 'application/x-www-form-urlencoded',
'Accept' => 'application/json'
],
]);
return Json::decode($response->getBody()->getContents());
$this->checkResponse($result);
} catch (GuzzleException $e) {
return $e->getMessage();
}
我试图找到一个发送请求并传输多个证书的示例。我找不到这样的例子,只使用一个证书不适合我
要向 Guzzle 请求添加多个证书,您可以使用请求选项数组中的 ssl_key 和 cert 选项。 ssl_key 选项可以设置为你的私钥文件的路径,cert 选项可以设置为你的证书文件的路径。
$certFile = 'cert.pem';
$keyFile = 'key.pem';
$caFile = 'client-bundle.pem';
$client = new GuzzleHttp\Client();
try {
$response = $client->request('POST', 'my_url', [
'headers' => [
'Content-Type' => 'application/x-www-form-urlencoded',
'Accept' => 'application/json'
],
'cert' => [$certFile, $keyFile],
'ssl_key' => $keyFile,
'verify' => $caFile
]);
$result = $response->getBody()->getContents();
$this->checkResponse($result);
return Json::decode($result);
} catch (GuzzleHttp\Exception\GuzzleException $e) {
return $e->getMessage();
}
使用 cat 命令,我将证书合并到一个文件中
cat *.pem >> cert-pem-bundle.pem
之后在cert参数中使用了生成的文件。最终功能:
try {
$response = $this->request('POST', 'my_url', [
'headers' => [
'Content-Type' => 'application/x-www-form-urlencoded',
'Accept' => 'application/json'
],
'cert' => ['/app/cert-pem-bundle.pem', 'testtest'], // testtest - pass phrase
]);
$result = Json::decode($response->getBody()->getContents());
return $result;
} catch (GuzzleException $e) {
return $e->getMessage();
}