php file_put_contents 在保存到文件时更改/编码字符串?
所以我遇到了一些奇怪的事情,我不能 100% 确定其原因是什么。
我正在创建一个脚本,用于登录到我的 ISP 并将返回的 JSON 中的安全令牌与当前时间 + 300 一起保存到文件中。HTTP
请求工作得很好,如果我回显数据,它会在浏览器中正确显示没有问题,但是当我尝试将所述数据保存到本地服务器上的文本文件时,它似乎正在被编码或其他东西。
这是返回的安全令牌
yJedovrH73bUz2Vuv2s8PPc3ju0U11eGaHMt73KLsYrb63Qe5IfZsz3BCAFv3vurasboPbiUjKXA0NlbE6lV0EWp2oZ8tnck9YODCHExCEmg9Gdv9dndyXkHAEaMDtJoGqEzie8_J9R7xg:071044
,071044 是当前时间 + 300,但是当我获取并查看文本文件时,我看到以下内容
YWE0ZjBkZGUtODdlMi00ZDlmLWJjODUtYmRmNzgzMjhlOThkMjAyMjAzMDIwNzExNTc5NzVEQjY0OTA3RUE1MEMxNjhBQ0YxMTZGNThENjVDNDA2MkFCN0JG33F1272C
我的代码在我的functions.php文件中如下所示,
<?php
include 'functions.php';
$AUTH_URL = "https://api.domain.com/auth/token";
header('Content-Type: text/html; charset=UTF-8');
$AUTH_HEADERS = array(
"authorization: Basic sdfcdfasdffcgr",
"Content-Type: application/x-www-form-urlencoded",
"Cookie: token=sdfcdfasdffcgr; expires=Tue, 15-Mar-2022 18:52:53 GMT; Max-Age=1209600; path=/; domain=api.domain.com; secure; HttpOnly",
);
$AUTH_POST = "type=token";
$AUTH_REQUEST = REQUEST($AUTH_URL, $AUTH_POST, $AUTH_HEADERS, 30);
$AUTH_JSON = JSON_DECODE($AUTH_REQUEST, TRUE);
$AUTH_TOKEN = $AUTH_JSON['token'];
$AUTH_EXPIRATION = date("his", time() + $AUTH_JSON['expires']);
$NEW_DATA = $AUTH_TOKEN . ':' . $AUTH_EXPIRATION;
file_put_contents('token.txt', $NEW_DATA );
ECHO $NEW_DATA;
?>
我有一个函数来发出curl请求
function REQUEST($URL, $POST_DATA, $HEADERS, $TIMEOUT) {
$REQUEST = curl_init();
curl_setopt($REQUEST, CURLOPT_URL, $URL);
if ($POST_DATA && !empty($POST_DATA)) {
curl_setopt($REQUEST, CURLOPT_POST, 1);
curl_setopt($REQUEST, CURLOPT_POSTFIELDS, $POST_DATA);
}
if ($HEADERS && !empty($HEADERS)) {
curl_setopt($REQUEST, CURLOPT_HTTPHEADER, $HEADERS);
}
curl_setopt($REQUEST, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($REQUEST, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($REQUEST, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($REQUEST, CURLOPT_CONNECTTIMEOUT, $TIMEOUT);
$DATA = curl_exec($REQUEST);
if (curl_errno($REQUEST)) {
return 'Error:' . curl_error($REQUEST);
}
curl_close($REQUEST);
return $DATA;
}
So i have something weird going on that i am not 100% sure what the reason for it is.
I am creating a script that logs into my ISP and saves the security token from the returned JSON to a file along with the current time + 300.
The HTTP request works perfectly fine and if i echo out the data it shows properly in the browser with no issues but when i try and save said data to a text file on my local server it seems to be getting encoded or something.
this is the security token returned
yJedovrH73bUz2Vuv2s8PPc3ju0U11eGaHMt73KLsYrb63Qe5IfZsz3BCAFv3vurasboPbiUjKXA0NlbE6lV0EWp2oZ8tnck9YODCHExCEmg9Gdv9dndyXkHAEaMDtJoGqEzie8_J9R7xg:071044
with 071044 being the current time + 300 but when i got and look in the text file i see the following
YWE0ZjBkZGUtODdlMi00ZDlmLWJjODUtYmRmNzgzMjhlOThkMjAyMjAzMDIwNzExNTc5NzVEQjY0OTA3RUE1MEMxNjhBQ0YxMTZGNThENjVDNDA2MkFCN0JG33F1272C
my code looks as follows
<?php
include 'functions.php';
$AUTH_URL = "https://api.domain.com/auth/token";
header('Content-Type: text/html; charset=UTF-8');
$AUTH_HEADERS = array(
"authorization: Basic sdfcdfasdffcgr",
"Content-Type: application/x-www-form-urlencoded",
"Cookie: token=sdfcdfasdffcgr; expires=Tue, 15-Mar-2022 18:52:53 GMT; Max-Age=1209600; path=/; domain=api.domain.com; secure; HttpOnly",
);
$AUTH_POST = "type=token";
$AUTH_REQUEST = REQUEST($AUTH_URL, $AUTH_POST, $AUTH_HEADERS, 30);
$AUTH_JSON = JSON_DECODE($AUTH_REQUEST, TRUE);
$AUTH_TOKEN = $AUTH_JSON['token'];
$AUTH_EXPIRATION = date("his", time() + $AUTH_JSON['expires']);
$NEW_DATA = $AUTH_TOKEN . ':' . $AUTH_EXPIRATION;
file_put_contents('token.txt', $NEW_DATA );
ECHO $NEW_DATA;
?>
inside of my functions.php file i have a single function to make the curl requests
function REQUEST($URL, $POST_DATA, $HEADERS, $TIMEOUT) {
$REQUEST = curl_init();
curl_setopt($REQUEST, CURLOPT_URL, $URL);
if ($POST_DATA && !empty($POST_DATA)) {
curl_setopt($REQUEST, CURLOPT_POST, 1);
curl_setopt($REQUEST, CURLOPT_POSTFIELDS, $POST_DATA);
}
if ($HEADERS && !empty($HEADERS)) {
curl_setopt($REQUEST, CURLOPT_HTTPHEADER, $HEADERS);
}
curl_setopt($REQUEST, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($REQUEST, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($REQUEST, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($REQUEST, CURLOPT_CONNECTTIMEOUT, $TIMEOUT);
$DATA = curl_exec($REQUEST);
if (curl_errno($REQUEST)) {
return 'Error:' . curl_error($REQUEST);
}
curl_close($REQUEST);
return $DATA;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论