-1

How to read in a JSON formatted POST http response from an external application webserver? I have it configured with the URL for my php script as the webhook URL.

Coding a PHP script that will sit on a server. I've passed the URL for this script to a webhook configuration in an external application. So this application server will send me the POST response when a certain event is triggered.

How do I read in the POST response if it's sent in JSON format? Is the JSON data in the $_POST variable? Or somewhere else?

1 Answers1

0

Accessing Posted Variables

$_POST Documentation

$_POST is a dictionary which contains all the posted variables. For example, if you posted a name variable then you can access it like this:

<?php
echo 'Hello ' . htmlspecialchars($_POST["name"]) . '!';
?>

The above example will output something similar to:

Hello Hannes!

Verifying Variables Are Set

isset() Documentation

Sometimes, you might receive invalid requests, so you should do some verification before you try and use the variables, so you don't run some function half way and then error because you weren't passed all the correct variables. You can you isset() to check if a variable is set. For example:

<?php
if (isset($_POST['name'])) {
    echo 'Hello ' . htmlspecialchars($_POST["name"]) . '!';
} else {
    echo 'You didn't set the name!';
}
?>

This will output Hello Hannes if name = Hannes and You didn't set the name! if name = null

Advice

Good luck with your project, but it seems like you don't know too much, and as a beginner I would recommend working on something more basic before jumping into webhooks, since they can be quite complex, and it's more beneficial to you, in the long run, to learn the basics first, since you will be able to piece together the more advanced code and learn it easier than just jumping right in.

Sources

http://php.net/manual/en/reserved.variables.post.php

http://php.net/manual/en/function.isset.php

Noah Cristino
  • 757
  • 8
  • 29