简体   繁体   中英

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? I need to include those files in the below http post request to be able to talk with the server.

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

I am able to do it using phpCurl like below:

$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. Any suggestion?

You can use Guzzle options provided by http client

$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.

use GuzzleHttp\Client;

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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