Parsing HTTP integration data in PHP

Hi
I need some hep with the HTTP integration , I have followed the youtube video. i have also read most of the posts of the http integrations . there is a lot of discussion around REST and MQTT as a use to collect the data from the TTN publish. However i woul d need to keep it a bit simpler due to the limitations of the server i am working with to retrieve the data.

So i have written some PHP code below to catch the data that is being sent to HTTP endpoint. however looking at the documentation on HTTP integration (https://www.thethingsnetwork.org/docs/applications/http/#uplink) the data is no where near the construct i expected

I am not a PHP expert , so some help will be greatly appreciated.

PHP code

<?php
	$filedta="ttndata/ttndata.txt";
	$ttndtajs=" ";
	$nxt="\r\n";

	// receive the JSON Post data
	$ttndtajs = json_decode( file_get_contents( 'php://input' ), true );

	file_put_contents($filedta,$ttndtajs, FILE_APPEND);
    //creating newline in text document as 
    //concatenation does not seem to work
	file_put_contents($filedta, $nxt, FILE_APPEND);
?>

My file is capturing the following

node_t135790ttn_node_00004A30B001BF96E334DZIAAAlzArrayArrayhttps://integrations.thethingsnetwork.org/ttn-eu/api/v2/down/node_t135790/ttndta32839?key=ttn-account-v2.secret

I am not sure what is the next step here

regards

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

To use that $ttndtajs object, see HTTP integration - #9 by I-Connect (and its replies).

1 Like

Hi,

Thank you very much for the explanation, I made the changes as suggested and now it is publishing the data as expected.

Major thanks to Arjan for fixing my gramer in the code block. could not find a button in the message box to do it correctly

Regards

And yes, this script is totally missing input sanitization, source verification etc: it is ok just to check functioning. Follow Arjan link for some explanation.

Hi Udlora
Quick question

$freq=$data->metadata->frequency; //shows the frequency nicely

i would like to read the rssi value as well, but the following is not returning any value.

$rrssi=$data->metadata->gateways->rssi;

Is there a different way that i should get the data out ?

Regards

Multiple gateways might have received the device’s transmission, so you’ll get an array in the JSON, each item being the details of one gateway. After parsing JSON into a PHP object, I assume you’ll also get an array. PHP is not my strong suit, but I’d assume something like:

// Not tested!
foreach($data->metadata->gateways as $gw) {
    $rssi = $gw->rssi;
    ...
}

For a surprise, see also https://www.thethingsnetwork.org/forum/t/can-one-gateway-be-listed-twice-in-the-ttn-metadata-gateways-array/12364

2 Likes

Arjan is right, both conceptually and syntactically :slight_smile:. At my very first attempt I assumed I was reaching just one gateway, thus I checked gateways[0] only, but with my surprise sometimes data was received by a 27km far gateway too. Better to look at the whole array…

Hi UdLora,
I tried this script:

However each time I send a (simulated) uplink message through HTTP POST to the endpoint (which is hosted at infinity free), only empty lines are written to ‘yourfile.txt’. (I do see the ‘ok’ message on the webpage).

When I use requestbasket, the body and header is non-empty. I also get a Cross-Origin Read Blocking (CORB) message in Chrome on the endpoint url. Perhaps the HTTP POST is blocked or something?
Thank you!

Are you only testing with Chrome? That doesn’t make sense; that will create a HTTP GET request, not a HTTP POST. You should configure the URL of your script in the integration.

To test without the integration, use something like Postman, which allows you to use POST rather than GET.

1 Like

I tested with opera as well. Same issue.
I will have a look at Postman.

I have simply entered the URL that I want to send to (where index.php is hosted), using the POST method.

Maybe the free hosting does not allow POST…? Any details on that hosting?

And when testing in a browser, does the URL change? (That might indicate that the hosting is sending some 301 Moved Permanently or 302 Found redirect, which might lose the POST content if the integration does not handle that correctly.)

1 Like

The URL doesn’t change, this is the warning in console :
Cross-Origin Read Blocking (CORB) blocked cross-origin response https://infinityfree.net/errors/404/ with MIME type text/html. See https://www.chromestatus.com/feature/5629709824032768 for more details.

It says here at infinityfree that InfinityFree cannot be used to host API endpoints, file or data storage and sharing services, bots, or hacking scripts. So that might be it…

1 Like

Yes, that’s it. And to achieve that, it injects JavaScript in your page. When seeing the OK in the browser, you can view the page source to see that JavaScript, I assume:

To enforce this, all visitors trying to access your website are checked to ensure Javascript and cookies are enabled.

(Although it’s weird that it actually creates some file contents, so your PHP is being executed…)

1 Like

This is what the page source looks like (developer mode --> elements):

<html><head></head><body><div id="StayFocusd-infobar" style="display:none;">
    <img src="chrome-extension://laankejkbhbdhmipfmgcngdelahlfoji/common/img/eye_19x19_red.png">
    <span id="StayFocusd-infobar-msg"></span>
    <span id="StayFocusd-infobar-links">
        <a id="StayFocusd-infobar-never-show">hide forever</a>&nbsp;&nbsp;|&nbsp;&nbsp;
        <a id="StayFocusd-infobar-hide">hide once</a>
    </span>
</div>ok
</body></html>

So the “ok” is in there…
So how should I go about it now? I just want to test the system, I don’t want to go out and have to buy a domain name/hosting service right away.

Hmmm, that source is mangled by some plugin; you’d better use some “View Page Source” instead. But even if you see the true response then that will not prove that the hosting does not support POST, as somehow it does execute your PHP…

Some alternatives at https://www.thethingsnetwork.org/labs/story/thethingsnetwork-lorawan-how-to-use-google-spreadsheet-to-log-data and Save data to a cloud server.

If you want to debug InfinityFree, you could create a PHP page that only includes the following, and then use Postman to test POST with some content in int body:

<?php
  // Show all information, defaults to INFO_ALL
  phpinfo();
?>

When testing your actual script with Postman, make sure to set the correct content type, like application/json, to use php://input.

Using view page source (ctrl + U) I just get the “ok” again.
image

Thanks ,will have a look at those!

I will have a look at that tomorrow.
Thanks a lot for you help :smile:

See also https://forum.infinityfree.net/discussion/1247/cant-receive-post-from-paypal-ipn and https://forum.infinityfree.net/discussion/1325/do-not-pass-a-post-with-json-request-to-the-server and https://forum.infinityfree.net/discussion/2685/php-file-does-not-output-json-format-for-post-request-from-c-client and …, which are running into the same problems… Not supported.