0

I'm trying to read data from a decompiled application of Android with a php server. I used wireshark to understand what types of data the application sends, and the result is:

{"initType":"first time","parameters":true,"details":................}

I trying to capture this data and insert them in a file with this php code:

<?php  
$json = $_POST["initType"];  
$decoded = json_decode($json, TRUE);  
if ($decoded === FALSE) {  
    throw new Exception('Bad JSON format.');  
}  
$file_handle = fopen('tmp.json', 'w');  
fwrite($file_handle, $decoded);  
fclose($file_handle);  
?> 

The file is correctly generated but it's empty. What is the error?

hasmet
  • 758
  • 3
  • 13
  • 32

2 Answers2

0

Because $decoded is an array, use a loop to write it to file or write the encoded JSON to file ($json). Try using this:

fwrite('tmp.json', print_r($decoded, TRUE));

or:

file_put_contents('tmp.json', print_r($decoded, TRUE));

Update (per your comments):

If you run a print_r($decoded) and it prints nothing, there is a problem with the decoding process of the JSON object passed in. I would recommend checking this to make sure it is formatted correctly. JSON formatting is a strict business and will halt your end goal if you are missing a double-quote or bracket. Start by echoing out $json ($_POST["initType"]) and compare the format to examples posted online (just Google "json formatting"). I can tell you that one thing that stands out to me is: "parameters":true (from your example above). I have a strong suspicion that the key true should be in double quotes. If you are positive that the JSON variable is correct syntactically, I don't think I would be of any more help. Using json_decode() to produce an array to very straight forward once you get it right.

burmat
  • 2,548
  • 1
  • 23
  • 28
  • I tried fwrite($file_handle, $json) and fwrite('tmp.json', print_r($decoded, TRUE)), but the result doesn't change. Only the first case the file is generated, so I think it means that `$json` contains no data. Other suggestions? – hasmet Jan 03 '13 at 17:15
  • Use print_r($decoded) to make sure it is decoded properly and dump the results of the array – burmat Jan 03 '13 at 17:19
  • What is the result then when you use print_r? Is it empty? Is there content? – burmat Jan 03 '13 at 17:43
  • it is empty, there is no data – hasmet Jan 03 '13 at 18:25
0

Don't decode the json at all -this only brings the problem of trying to serialize an array that burmat mentioned in his answer.

Write to the file the json content straight away:

fwrite($file_handle, $json);

Also, though I am not PHP expert it seems you access the body of the post request wrongly. Please refer to the following post.

Community
  • 1
  • 1
Boris Strandjev
  • 46,145
  • 15
  • 108
  • 135
  • I tried: fwrite($file_handle, $json); and fwrite('tmp.json', print_r($decoded, TRUE)); but the result doesn't change. Only the first case the file is generated, so I think it means that `$json` contains no data. Other suggestions? – hasmet Jan 03 '13 at 17:14
  • @hasmet: have you seen my edit about how you should read the body of post request? – Boris Strandjev Jan 04 '13 at 15:02