0

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;
}
user72261
  • 11
  • 1
  • 5
  • Read more here, might be relevant ... https://stackoverflow.com/questions/11115533/php-file-put-contents-and-utf-8 ... And although that thread was closed, there is mention in there of `UTF-8` being lost because how the hosting provider's server was configured on encoding. – GetSet Mar 01 '22 at 19:25
  • I'd avoid using variable names like `$_POST` and `$_REQUEST` for your own data. These are PHP super globals. I think you're alright here, but the variable names are confusing, and they might not contain what you expect. – Tangentially Perpendicular Mar 01 '22 at 19:33
  • @GetSet this issue is not content encoding related i dont think as i have another script on the server that does almost the exact same thing and that is working for some reason it only seems to be this file doing it – user72261 Mar 01 '22 at 19:38
  • @TangentiallyPerpendicular yeh i usualy have the name of the current file before the veriable names like login_url or categories_url and stuff but i am currently just testing this to try and work out why this is messing up but other files on the server seem to be working perfect – user72261 Mar 01 '22 at 19:39
  • As always, my advice is to Break The Problem Down. Dump out the value of `$NEW_DATA`, then hard-code that value at the top of a file, run the `file_put_contents` line, and nothing else. PHP doesn't care where data comes _from_, so the rest of the code should be completely irrelevant. Also, check for silly mistakes: are you definitely looking at the right file when you check the result? Try writing to a different file name, and specify a full disk path, just to make doubly sure. – IMSoP Mar 01 '22 at 21:07

0 Answers0