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);
?>
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:
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.
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;
...
}
Arjan is right, both conceptually and syntactically . 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âŚ
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.
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.)
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âŚ
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âŚ)
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âŚ
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.