2

Is it possible to create a POST request using guzzle6 which has .pfx-certificate attached to it?

The documention only mentions pem-format: http://docs.guzzlephp.org/en/stable/request-options.html#cert

jjei
  • 1,180
  • 3
  • 13
  • 21
  • 2
    Just convert the [`.pfx` file to `.pem`](https://stackoverflow.com/questions/15413646/converting-pfx-to-pem-using-openssl). – Alexey Shokov Dec 05 '17 at 18:41
  • I tried this but using pem format didn't work out in my context. Apparently there are some problems with mac os x and pem certificate https://stackoverflow.com/questions/40712352/curl-error-58-ssl-cant-load-the-certificate-and-its-private-key-osstat – jjei Dec 14 '17 at 08:48

3 Answers3

2

Although the documentation at http://docs.guzzlephp.org/en/stable/request-options.html#cert doesn't mention it, it seems to be possible to also use pfx-format with guzzle.

jjei
  • 1,180
  • 3
  • 13
  • 21
0

PFX certificates are used for "mutual authentications", that means, the PFX is generated with your local private key and the remote public cert.

To generate a PFX key you run:

openssl pkcs12 -inkey your_privkey.pem -in remote_pub.cert -export -out mixed.pfx

To make a request using the PFX cert, you can:

$api = new \GuzzleHttp\Client([
    'base_uri' => $baseUrl,
    'cert'     => 'path/to/mixed.pfx',
    'curl'     => [CURLOPT_SSLCERTTYPE => 'P12'], // to define it's a PFX key
]);
StR
  • 545
  • 6
  • 17
0

this will work in drupal 8 too

use GuzzleHttp\Client;

 // Base URI is used with relative requests

$client = new Client([
   
        'base_uri' => 'https://www.google.com',
        'cert'     => 'pathtopfxflie/nameof.pfx',
        'curl'     => [CURLOPT_SSLCERTTYPE => 'P12']]);
$response = $client->request('METHOD', 'api path',['headers' => ['Employer' => 100]]);

//get status code using $response->getStatusCode();

$body = $response->getBody();
$arr_body = json_decode($body);
Suraj Rao
  • 29,388
  • 11
  • 94
  • 103
Vinod D
  • 1
  • 1