October 22, 2024
Chicago 12, Melborne City, USA
PHP

PHP cURL unable to set private key file: 'C:\…\cacert.pem' type PEM


I’m trying to run the PHP cURL request example of an API from a Windows/IIS web server
Here is the code:

<?php
$curl = curl_init();
curl_setopt($curl, CURLOPT_SSLCERT, 'C:\...\cacert.pem');
curl_setopt($curl, CURLOPT_VERBOSE, true);
$streamVerboseHandle = fopen('php://temp', 'w+');
curl_setopt($curl, CURLOPT_STDERR, $streamVerboseHandle);
curl_setopt_array($curl, [
    CURLOPT_URL => $url,
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_ENCODING => "",
    CURLOPT_MAXREDIRS => 10,
    CURLOPT_TIMEOUT => 30,
    CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
    CURLOPT_CUSTOMREQUEST => "POST",
    CURLOPT_POSTFIELDS => $body,
    CURLOPT_HTTPHEADER => [
        "Authorization: Basic ".$username.":".$password,
        "Message-Reference: SOME_STRING_VALUE",
        "Message-Reference-Date: SOME_STRING_VALUE",
        "Plugin-Name: ",
        "Plugin-Version: ",
        "Shipping-System-Platform-Name: ",
        "Shipping-System-Platform-Version: ",
        "Webstore-Platform-Name: ",
        "Webstore-Platform-Version: ",
        "content-type: application/json"
    ],
]);
$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);
if ($err) {echo "cURL Error #:" . $err."<br>";} else {echo $response;}
rewind($streamVerboseHandle);
$verboseLog = stream_get_contents($streamVerboseHandle);
echo "cUrl verbose information:\n", 
"<pre>", htmlspecialchars($verboseLog), "</pre>\n";
?>

In my php.ini I have the line:

curl.cainfo="C:\...\cacert.pem"

But for some reason I had to add the CURLOPT_SSLCERT option because I was getting:

OpenSSL SSL_read: SSL_ERROR_SYSCALL, errno 10054

I downloaded the cacert.pem file from here as written in this note

But I get this:

cURL Error #:unable to set private key file: 'C:\...\cacert.pem' type PEM
cUrl verbose information:
*   Trying xxx.xxx.xxx.xxx...
* TCP_NODELAY set
* Connected to api.domain.ext (xxx.xxx.xxx.xxx) port 443 (#0)
* ALPN, offering http/1.1
* unable to set private key file: 'C:\...\cacert.pem' type PEM
* Closing connection 0

Am I missing something? How can I solve?



You need to sign in to view this answers

Leave feedback about this

  • Quality
  • Price
  • Service

PROS

+
Add Field

CONS

+
Add Field
Choose Image
Choose Video