Handling POST data from IoT device in PHP web app

I’m new to web dev and trying to build a simple app. It’s supposed to get data from my Particle Photon board through a webhook. Every time something happens on the board, it sends JSON data via POST to my Apache server. I want to use PHP to deal with this POST request and show the info to users.

Right now, I just have an index.php file with this code:

<html>
<body>
  <div>
    <?php
    if ($_SERVER['REQUEST_METHOD'] == 'POST') {
      $incoming = json_decode(file_get_contents('php://input'));
      // process $incoming here
    }
    ?>
  </div>
</body>
</html>

I’m not bothered about checking the input yet. I just want it to work basically. I thought the webhook would send the POST to index.php (which seems to be happening), and then index.php would show the POST info. But I can’t get it to work. I’ve tried var_dump and echo, but I get nothing or NULL.

Am I missing something big here? The JSON being sent looks okay. index.php is the only file on my server. Any ideas what I’m doing wrong?

It seems like you’re on the right track, but there might be a few things to check. First, ensure your webhook is correctly configured to send data to your server’s exact URL. Also, try adding error reporting at the top of your PHP file:

error_reporting(E_ALL);
ini_set('display_errors', 1);

This will help you see any PHP errors that might be occurring. Additionally, you could try storing the raw POST data in a variable before decoding it:

$raw_data = file_get_contents('php://input');
$incoming = json_decode($raw_data, true);

Then, echo both $raw_data and $incoming to see what’s actually being received. If you’re still not seeing anything, it might be worth checking your server’s error logs for any clues. Remember, debugging IoT data can take some patience, but you’ll get there!

hey there! have u considered using a logging system to see whats actually coming in? maybe try adding this at the top of ur php file:

file_put_contents('log.txt', file_get_contents('php://input'), FILE_APPEND);

this could help u figure out if the data is even reaching ur server. what do u think?

yo Leo, sounds like ur on the right track! have u tried checking ur apache error logs? they might give u some clues. also, make sure ur php.ini is set up to show errors. sometimes the data comes in but php is hiding issues. good luck man!