简体   繁体   English

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

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

How to pass client certificate (2 files .key and .pem) through http client request?如何通过 http 客户端请求传递客户端证书(2 个文件 .key 和 .pem)? I need to include those files in the below http post request to be able to talk with the server.我需要在下面的 http post 请求中包含这些文件才能与服务器通信。

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

I am able to do it using phpCurl like below:我可以使用如下所示的 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)];
        }

But I need to do it using Http Client for many other purposes.但我需要使用 Http Client 来实现许多其他目的。 Any suggestion?有什么建议吗?

You can use Guzzle options provided by http client您可以使用 http 客户端提供的Guzzle 选项

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

It would be same as using ssl_key request option for guzzle http client instance directly.这与直接为 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']
]);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM