26

I have a php script in my Apache server that have to send a curl request to a partner's server. Partner give me a .pem file that I have to attach to every call I do to its api.

My php script is the follow:

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_SSLCERT, "test.pem" );
curl_setopt($ch,CURLOPT_SSLCERTTYPE,"PEM");

curl_setopt($ch, CURLOPT_RETURNTRANSFER, True);
curl_setopt($ch, CURLOPT_POST, True);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_POSTFIELDS, $json);
curl_setopt($ch, CURLOPT_VERBOSE, true);

$result = curl_exec($ch);

if(!$result)
{
    echo "Curl Error: " . curl_error($ch);
}
else
{
    echo "Success: ". $result;
}

curl_close($ch);

It returns:

Curl Error: unable to set private key file: 'test.pem' type PEM

Consider that it sends me .pem file and says "it has no passphrase"

2 Answers 2

18
+50

I think that you need to use the tmpfile() and stream_get_meta_data.

$pemFile = tmpfile();
fwrite($pemFile, "test.pem");//the path for the pem file
$tempPemPath = stream_get_meta_data($pemFile);
$tempPemPath = $tempPemPath['uri'];
curl_setopt($ch, CURLOPT_SSLCERT, $tempPemPath); 

Source: This answer here in SO helps me with similar problem.

3
  • Hi James, thanks very much!!! I implemented the code and now it report: "Curl Error: could not load PEM client certificate, OpenSSL error error:0906D06C:PEM routines:PEM_read_bio:no start line, (no key found, wrong pass phrase, or wrong file format?)" Do you know what it means?
    – user3914418
    Nov 26, 2015 at 13:14
  • The problem could be your pem file. Are you on a windows environment ? See if this query helps you. Ensure that your file starts with -----BEGIN PUBLIC KEY-----, with no spaces.
    – James
    Nov 26, 2015 at 13:33
  • 1
    Thanks very much James!
    – user3914418
    Nov 26, 2015 at 15:11
1

I think you're missing curl_setopt($ch, CURLOPT_CAINFO, 'test.pem'); Have a look at cURL is unable to use client certificate , in local server for more about using client certificates in curl via PHP

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.