Passing .PEM and .KEY as string in Curl using PHP Passing .PEM and .KEY as string in Curl using PHP curl curl

Passing .PEM and .KEY as string in Curl using PHP


The answer is unfortunately as easy as it is simple: No, it is not possible.

The underlying libcurl has no API for providing keys as strings, only as files!

Bonus material:

If you're sure that your libcurl is built with OpenSSL, you can actually use the CURLOPT_SSL_CTX_FUNCTION option to do it. However:

  1. that makes it an libcurl+OpenSSL specific solution

  2. I don't think PHP/CURL exposes that function (enough) to allow this. You would probably need to extend the binding code first...

(I should add that I am the main author and maintainer of libcurl.)


Using tmpfile() might suffice as a workaround.

$tempPemFile = tmpfile();fwrite($tempPemFile, $pemfile);$tempPemPath = stream_get_meta_data($tempPemFile);$tempPemPath = $tempPemPath['uri'];

and then:

curl_setopt($ch, CURLOPT_SSLCERT, $tempPemPath); 

but make sure you close it after so the tmp file is delete

fclose($tempPemFile);


You could create temporary files, write the strings into the files and then point to the temp files...