简体   繁体   English

在php文件中使用file_get_contents定义变量

[英]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. 我正在使用ServerPilot API将自签名SSL证书应用于ServerPilot应用程序。 I already have the csr, key, and crt files ready to go. 我已经准备好了csr,key和crt文件。 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. 现在,我使用下面提供的脚本将密钥和crt txt文件的内容保存到一个变量,然后可以在脚本中使用该变量保存到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. 有两个问题:1.在$ sslkey和$ sslcert变量上使用file_get_contents在路径中有另一个变量。 When I run the php script it returns this: 当我运行php脚本时,它返回以下内容:

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. 如果我在$ sslkey和$ sslcert变量中手动输入域名进行测试。 The PHP warnings do not happen, but the script does not execute. PHP警告不会发生,但是脚本不会执行。

If I test echoing the variables, the file contents of the two ssl files return to the screen just fine. 如果我测试回显变量,则两个ssl文件的文件内容将返回屏幕。

Issue #1 第1期

$vardomainname has an incorrect string. $vardomainname的字符串不正确。 Missing characters, too many characters , hidden characters, or just plain wrong; 缺少字符, 太多字符 ,隐藏字符或完全错误; idk. idk。

or 要么

"/root/certs/$vardomainname/ssl.key" does not exist. "/root/certs/$vardomainname/ssl.key"不存在。

or 要么

You do not have access to "/root/certs/$vardomainname/ssl.key" 您无权访问"/root/certs/$vardomainname/ssl.key"


Issue #2 第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. 根据curl_setopt()CURLOPT_POSTFIELDS部分,您需要提供一个PHP数组或将urlencode()适当地提供给一个字符串。 JSON is not appropriate here. JSON在这里不合适。


Last but not least 最后但并非最不重要的

You are blind without curl_errno and curl_error() 您是没有curl_errnocurl_error()盲人

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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