I have two files for SSLssl_crt
andssl_key
. I want to read the content of these files and send it to the APIs as a JSON field. But thefile_get_contents
actually convert the single slash to double so the API return the following error.
The private key format is invalid.
The certificate format is invalid.
I am actually using run cloud service where I want to send the content of these files to one of their API
https://runcloud.io/docs/api/web-application#ssl-basic-install-ssl
As you can see in the above URL, they need the content in the proper format.
So I tried the following.
$curl = curl_init();
$ssl_crt = file_get_contents(selectServer()->ssl_crt_path);
$ssl_key = file_get_contents(selectServer()->ssl_key_path);
$data = [
'provider' => 'custom',
'enableHttp' => true,
'enableHsts' => false,
'certificate' => $ssl_crt,
'privateKey' => $ssl_key,
];
curl_setopt_array($curl, array(
CURLOPT_URL => 'https://manage.runcloud.io/api/v2/servers/{id}/webapps/{webapp_id}/ssl',
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => '',
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 0,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => 'POST',
CURLOPT_POSTFIELDS => json_encode($data, JSON_UNESCAPED_SLASHES),
CURLOPT_HTTPHEADER => array(
'Accept: application/json',
'Content-Type: application/json',
'Authorization: Basic {token}'
),
));
$response = curl_exec($curl);
curl_close($curl);
echo $response;
If I manually write the content of the certificate(or key) to the data array then it works fine. i.e
$data = [
// ...
'certificate' => '-----BEGIN CERTIFICATE-----\nblah\nblah==\n-----END CERTIFICATE-----,
'privateKey' => '-----BEGIN PRIVATE KEY-----\nblah\nblah==\n-----END PRIVATE KEY-----,
];
The actual issue with the format. the "\n" becomes "\n" and the main content of the certificate or key can have "\n".
Can someone help me out with this?
the post-mortem here is that the on-disk file was corrupted, the on-disk file had all newlines converted to literal-slash-followed-by-n, which PHP's double-quote operator was fixing, thus it worked when you hardcoded the file (with php's double-quote operator. it would not have worked with php's single-quote operator), and didn't work when you got it from file_get_contents() - code used to fix the on-disk file was
<?php
$real_crt=file_get_contents("ssl_crt");
$fixed_crt = strtr($real_crt,array(
"\\n"=>"\n",
"\\r"=>"\r",
));
file_put_contents("ssl_crt_fixed",$fixed_crt);
i only figured this out after you sent me the ssl_cert file on facebook messenger, though, and i'm pretty sure the ssl_cert file was required to solve this mystery, so i have voted to close this question asrequires the shortest code necessary to reproduce the problem.
ie, it needs the ssl_cert file to be any kind of reproducible
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Find the answer in similar questions on our website.
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.
PHP (from the English Hypertext Preprocessor - hypertext preprocessor) is a scripting programming language for developing web applications. Supported by most hosting providers, it is one of the most popular tools for creating dynamic websites.
The PHP scripting language has gained wide popularity due to its processing speed, simplicity, cross-platform, functionality and distribution of source codes under its own license.
https://www.php.net/
Welcome to the Q&A site for web developers. Here you can ask a question about the problem you are facing and get answers from other experts. We have created a user-friendly interface so that you can quickly and free of charge ask a question about a web programming problem. We also invite other experts to join our community and help other members who ask questions. In addition, you can use our search for questions with a solution.
Ask about the real problem you are facing. Describe in detail what you are doing and what you want to achieve.
Our goal is to create a strong community in which everyone will support each other. If you find a question and know the answer to it, help others with your knowledge.