Parsing HTTP integration data in PHP

You are doing a json_decode and then saving, and as text, which is not appropriate because $ttndtajs is no more text but an object. You can use var_export() to obtain a parsable serialization of that object, however is is like saving JSON directly.

When I started using TTN, I used this small PHP script to log raw (i.e., JSON) data from nodes:

<?php 
    //Receive from TTN
    $postdata = file_get_contents('php://input');
    file_put_contents('yourfile.txt',  $postdata . PHP_EOL, FILE_APPEND);
    echo 'ok';
?>

Then I have another script that reads and decodes each line of such file, showing data on an HTML table. This includes something like:

$log=file('yourfile.txt');
foreach($log as $r) {
	$data=json_decode($r); 
	$dev_id =$data->dev_id;
	$HW=$data->hardware_serial;
    $freq=$data->metadata->frequency;
    $datarate=$data->metadata->data_rate;
    ...
}
1 Like