I've written the following, using JSON, for a chat system:
header('Content-type: application/json');
$theName = $_GET['name'];
$theMessage = $_GET['message'];
$str = file_get_contents("transactions.json");
if ($str == ""){
$str = '[]';
}
$arr = json_decode($str, true);
$arrne['name'] = "$theName";
$arrne['message'] = "$theMessage";
array_push($arr, $arrne);
$aFinalTransaction = json_encode($arr, JSON_PRETTY_PRINT);
echo $aFinalTransaction;
file_put_contents("transactions.json", "$aFinalTransaction");
This gets the content of the transactions file, decodes it, insert the name and messages taken from $_GET, pushes it into the array and encodes it back into a string and puts it into the transactions file.
This works fine, however I need to do the exact same thing just for XML instead of JSON. So the string from the file would look something like this:
<chatMessage>
<name>Name1</name>
<message>Msg1</message>
</chatMessage>
<chatMessage>
<name>Name2</name>
<message>Msg2</message>
</chatMessage>
This is how is looks right now in JSON:
[
{
"name": "Name1",
"message": "Msg1"
},
{
"name": "Name2",
"message": "Msg2"
}
]
Also, one more thing, how exactly would I name that JSON object? So it'd look something like this instead:
{"messages":[
{
"name": "Name1",
"message": "Msg1"
},
{
"name": "Name2",
"message": "Msg2"
}
]}
This last part is probably easier than I think, but I haven't had any luck.
I really hope you can help me. Thanks.