0

I am working with Webhook to receive data, so I can store it into database, so I got data as Json and tried to decode it, so I can retrieve each one using json_decode and save them in a text file but I got nothing the file was empty
so here is me code :

<?php
require_once('vendor/autoload.php');


$payload = @file_get_contents('php://input');
if (isset($payload) && !empty($payload)) {

    $data = json_decode($payload, true);
    $myFile2 = "file.txt";
    $myFileLink2 = fopen($myFile2, 'w+') or die("Can't open file.");
    $newnbord = $data;
    fwrite($myFileLink2, $newnbord);
    fclose($myFileLink2);
}

and this is an example of data I received through webhook :

{"accountingCurrency":"USD","address1":"test address","billedCurrency":"EUR","billedRecurringPrice":"0.00","bin":"444444","cardType":"VISA","city":"testcity","country":"FR","initialPeriod":"30","lastName":"test","timestamp":"2020-11-12 06:05:49","username":"llllll","threeDSecure":"NOT_APPLICABLE"}
elaaa
  • 19
  • 1
  • 11
  • Your json here looks correct, but if json_decode returned false, then it didn't like something. Check for [json_last_error_msg](https://www.php.net/manual/en/function.json-last-error-msg.php) after you decode to see if there are any errors – aynber Nov 12 '20 at 13:14
  • 4
    You shouldn't decode the json, save it to the file as it is. Text documents can contain text, the string you receive is a text. – Ivanka Todorova Nov 12 '20 at 13:14
  • @aynber json_last_error_msg returned Malformed UTF-8 characters, possibly incorrectly encoded – elaaa Nov 12 '20 at 13:17
  • @IvankaTodorova I am saving it into a file just to test it I need to decode it so I decompose it and store it into a database – elaaa Nov 12 '20 at 13:19
  • You might want to look into https://stackoverflow.com/questions/46305169/php-json-encode-malformed-utf-8-characters-possibly-incorrectly-encoded to see if that helps – aynber Nov 12 '20 at 13:25
  • @aynber well I did try that, I used mb_convert_encoding($payload, 'UTF-8', 'UTF-8'); I got no more errors and got an empty array in my text file – elaaa Nov 12 '20 at 13:29
  • 1
    @elaaa The code you pasted says that you are saving the decoded version of the json. If you want to test it out, just save the contents of `$payload` or `fwrite($myFileLink2, var_export($newnbord))`. If you look at [`fwrite()`](https://php.net/fwrite) at php.net, the second argument is expected to be a string. – Ivanka Todorova Nov 12 '20 at 14:23
  • Thank you all for trying to help me but I still got nothing – elaaa Nov 12 '20 at 14:34
  • `fwrite($myFileLink2, $payload);` would make more sense here. No need to decode text into an object if all you're going to do is just save it straight into a text file. – ADyson Nov 12 '20 at 14:50
  • I knew this `fwrite($myFileLink2, $payload);` will get me the response in my text file. But what I need to take is for example take the value of accountingCurrency and store it into a data base – elaaa Nov 12 '20 at 15:00
  • So why are you worrying about empty files then? Ask a question with relevant database code, if that's your real problem – ADyson Nov 12 '20 at 15:56

0 Answers0