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"

Machavity
  • 30,841
  • 27
  • 92
  • 100

2 Answers2

18

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.

Community
  • 1
  • 1
James
  • 1,653
  • 2
  • 31
  • 60
  • 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? –  Nov 26 '15 at 13:14
  • The problem could be your pem file. Are you on a windows environment ? See if this [query](http://stackoverflow.com/questions/20837161/openssl-pem-routinespem-read-biono-start-linepem-lib-c703expecting-truste) helps you. Ensure that your file starts with `-----BEGIN PUBLIC KEY-----`, with no spaces. – James Nov 26 '15 at 13:33
  • 1
    Thanks very much James! –  Nov 26 '15 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

Community
  • 1
  • 1
in need of help
  • 1,606
  • 14
  • 27