PHP Curl Request with Certificate (cert pem file option) Example

By Hardik Savani May 14, 2024 Category : PHP

Hi,

This tutorial shows you php curl request with certificate. if you have question about php curl request with pem certificate then i will give simple example with solution. Here you will learn php curl request pass pem file. you will learn how to send a curl request with pem certificate in php. follow bellow step for php send curl request with pem file certificate.

In this example i will give you example of how to send curl request with pem file certificate authentication request. you can pass pem file certificate as like bellow:

curl_setopt($ch, CURLOPT_SSLCERT, "mycert.pem");

curl_setopt($ch, CURLOPT_SSLCERTTYPE, "PEM");

Example:

<?php

$ch = curl_init();

curl_setopt($ch, CURLOPT_URL, 'https://example.com/token');

curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

curl_setopt($ch, CURLOPT_POST, 1);

curl_setopt($ch, CURLOPT_POSTFIELDS, "grant_type=client_cert");

curl_setopt($ch, CURLOPT_SSLCERT, "mycert.pem");

curl_setopt($ch, CURLOPT_SSLCERTTYPE, "PEM");

$headers = [];

$headers[] = 'Content-Type: application/x-www-form-urlencoded';

curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);

$result = curl_exec($ch);

if (curl_errno($ch)) {

return false;

}

curl_close($ch);

$result = json_decode($result);

print_r($result);

?>

i hope it can help you...

Tags :
Shares