如何在 php laravel 8 中通过 HTTP 客户端传递客户端证书

How to pass client certificate through HTTP Client in php laravel 8

如何通过http客户端请求传递客户端证书(2个文件.key和.pem)? 我需要在下面的 http post 请求中包含这些文件才能与服务器通信。

$response = Http::post('https://domainname.com/api/client/session', array('xxx' => array('xx' => 'xxxx')));

我可以像下面这样使用 phpCurl 来做到这一点:

$curl = curl_init();
        curl_setopt_array($curl, array(
            CURLOPT_URL => $url,
            CURLOPT_RETURNTRANSFER => true,
            CURLOPT_ENCODING => '',
            CURLOPT_MAXREDIRS => 10,
            CURLOPT_TIMEOUT => 0,
            CURLOPT_FOLLOWLOCATION => true,
            CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
            CURLOPT_CUSTOMREQUEST => $type,
            CURLOPT_POSTFIELDS => $body,
            CURLOPT_HTTPHEADER => $header,
            CURLOPT_SSLKEY => $pemPath,
            CURLOPT_SSLCERT => $crtPath,
            // CURLOPT_NOSIGNAL => 1

        ));

        $response = curl_exec($curl);


        $err = curl_error($curl);

        curl_close($curl);

        echo (json_encode($response));
        echo ("\n\n");
        if ($err) {
            return ["success" => false, "message" => $err];
        } else {
            return ["success" => true, "data" => json_decode($response)];
        }

但出于许多其他目的,我需要使用 Http Client 来完成此操作。有什么建议吗?

可以使用http客户端提供的Guzzle options

$response = Http::withOptions([
    'ssl_key' => ['/path/to/cert.pem', 'password.key']
])->post('https://domainname.com/api/client/session');

这与直接对 guzzle http 客户端实例使用 ssl_key 请求选项相同。

use GuzzleHttp\Client;

$client = new Client();
$client->request('POST', 'https://domainname.com/api/client/session', [
    'ssl_key' => ['/path/to/cert.pem', 'password.key']
]);