简体   繁体   中英

PHP Curl request with CRT, PEM, PFX and Passphrase

I used below code in PHP. I searched few sites and worked this below curl function. In Postman Add Certificate (Settings->SSL Verification Settings) it is working by passing CRT,PEM,PFX and Passphrase. But when I'm going to use this in PHP code it is not working.

function curlapi() {
$url = "https://exampleurl.com/service";
$curl = curl_init($url);
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);

$headers = array(
   "Content-Type: application/json",
);
$data = '{"login":"test_login","password":"test_password"}';
$pemFile = tmpfile();
fwrite($pemFile, "/folder/path/test_file.pem");//the path for the pem file
$tempPemPath = stream_get_meta_data($pemFile);
$tempPemPath_uri = $tempPemPath['uri'];
curl_setopt($curl, CURLOPT_SSLCERT, $tempPemPath_uri);
curl_setopt($curl, CURLOPT_POSTFIELDS, $data);

//for debug only!
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);

$resp = curl_exec($curl);
echo "<pre>";
print_r(curl_getinfo($curl)) . '<br/>';
echo curl_errno($curl) . '<br/>';
echo curl_error($curl) . '<br/>';
curl_close($curl);
print_r($resp);
exit;
}
curlapi();

Finally, for my concern there is no usage of CRT, PFX & Passphrase in CURL request. It works only with PEM file path.

  <?php $data =  array(
    "PageNumber"        => 1,
    "PageSize"        => 100,
    "SearchCriteria"         => array(
      "keyword"         => "Getvalue"
    ));
    $pemfile = getcwd()."/assets/path/file.pem";    $curl = curl_init($url);
    $method = "POST";
    curl_setopt($curl, CURLOPT_URL, $url);
    curl_setopt($curl, CURLOPT_POST, true);
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($curl, CURLOPT_CAINFO, $pemfile);
    curl_setopt($curl, CURLOPT_SSLCERT, $pemfile);
    curl_setopt($curl, CURLOPT_CUSTOMREQUEST, $method);    
    curl_setopt($curl, CURLOPT_POST, true);
    $headers = array(
       "Content-Type: application/json",
    );
    curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
    curl_setopt($curl, CURLOPT_POSTFIELDS, json_encode($data));
    $resp = curl_exec($curl);
    /*echo "<pre>";
    print_r(curl_getinfo($curl)) . '<br/>';
    echo curl_errno($curl) . '<br/>';
    echo curl_error($curl) . '<br/>';*/
    curl_close($curl);
?>

I know I am replying this very late, but CURL request does support PFX File format with Key, Below is the code for the same

 Curl::to($url)
        ->withHeaders($headers)
        ->withContentType('application/json')
        ->withOption('HTTP_VERSION', 'CURL_HTTP_VERSION_1_1')
        ->withOption('SSLCERTTYPE', 'P12')
        ->withOption('SSLCERT', <Path of the Certificate>)
        ->withOption('SSLKEYPASSWD', <passphrase used to create pfx>)
        ->withOption('HTTP_VERSION', 'CURL_HTTP_VERSION_1_1')
        ->withData(json_encode($body))
        ->withTimeout(30)
        ->post();

The main key of sending PFX file with curl request is telling CURL you are passing P12 format file for that purpose we are using:

'SSLCERTTYPE', 'P12'

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