-1

I trying to dump all that was HTTP POST in json to a text file call logs.txt, the file can be written and

<?php
$myfile = fopen("/var/www/html2/inc/logs/logs.txt", "a") or die("Unable to open file!");
$data = json_decode($_POST, true);

fwrite($myfile, "\n".$data);
fclose($myfile);
?>

The result was actually empty.. nothing was written. I did try if the file is writeable and yes it created the file, no folder permission issue, I think of using serialize but its not working.

  • 1
    `$_POST` is an array, trying to use `json_decode` on that makes no sense to begin with. If your PHP script was actually send data _as_ JSON, then you won’t find this in $_POST to begin with. How to handle that, see https://stackoverflow.com/questions/18866571/receive-json-post-with-php – misorude Jan 21 '19 at 13:14

1 Answers1

1

json_decode() expects parameter 1 to be string, array given might be the error.

To convert an array to a JSON you need to use:

$json = json_encode($_POST);

http://php.net/manual/de/function.json-encode.php

  • While these links may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes. – Jay Blanchard Jan 21 '19 at 13:09
  • The json_decode() function returns an array, not an object, when the flag for associative array is set to true. – Jay Blanchard Jan 21 '19 at 13:11
  • Yeah I missed this. Just saw it. –  Jan 21 '19 at 13:12