简体   繁体   中英

Using file_get_contents in php file to define variable

I am using the ServerPilot API to apply a self-signed SSL certificate to a ServerPilot App. I already have the csr, key, and crt files ready to go. Now I am using the supplied script below to save the contents of the key and crt txt files to a variable which then can be used in the script to save to the ServerPilot App.

#!/usr/bin/php

<?php
$clientid = "redacted";
$apikey = "redacted";

$appid = file_get_contents("temp_serverpilot-appid_filtered.txt");

$vardomainname = file_get_contents("temp_serverpilot-domainname.txt");

$sslkey = file_get_contents("/root/certs/$vardomainname/ssl.key");

$sslcert = file_get_contents("/root/certs/$vardomainname/ssl.crt");

$data = array(
    "key" => $sslkey,
    "cert" => $sslcert,
    "cacerts" => null
);
$data_string = json_encode($data);

$ch = curl_init("https://api.serverpilot.io/v1/apps/$appid/ssl");
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($ch, CURLOPT_USERPWD, "$clientid:$apikey");
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
    'Content-Type: application/json',
    'Content-Length: ' . strlen($data_string))
);


$result = curl_exec($ch);
curl_close($ch);

echo json_encode(json_decode($result), JSON_PRETTY_PRINT);

There are two problems: 1. Using file_get_contents on the $sslkey and $sslcert variables has another variable in the path. When I run the php script it returns this:

PHP Warning:  file_get_contents(/root/certs/mydomainredacted.com/ssl.key): failed to open stream: No such file or directory in /root/serverpilot-ssl-apply.php on line 11
Warning: file_get_contents(/root/certs/mydomainredacted.com/ssl.key): failed to open stream: No such file or directory in /root/serverpilot-ssl-apply.php on line 11
  1. If I manually enter the domain name in the $sslkey and $sslcert variables to test. The PHP warnings do not happen, but the script does not execute.

If I test echoing the variables, the file contents of the two ssl files return to the screen just fine.

Issue #1

$vardomainname has an incorrect string. Missing characters, too many characters , hidden characters, or just plain wrong; idk.

or

"/root/certs/$vardomainname/ssl.key" does not exist.

or

You do not have access to "/root/certs/$vardomainname/ssl.key"


Issue #2

Change:

curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);

into

curl_setopt($ch, CURLOPT_POSTFIELDS, $data);

Per the CURLOPT_POSTFIELDS section of curl_setopt() you need to provide either a PHP array or properly urlencode() the array into a string. JSON is not appropriate here.


Last but not least

You are blind without curl_errno and curl_error()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM