Jodo1992 Jodo1992 - 1 year ago 153
JSON Question

How do I get data from Webhook to store in a database?

I am working with a Webhook based of of SendGrid's API v3. The Webhook is completely set up with an endpoint that SendGrid posts to, but I need to be able to receive that parsed data and store it in a database using PHP, but do not know how to do that.

I have worked with APIs before where I initiate the retrieval or POST of data, but when the API server is the one POSTing, how do I catch the data being parsed through the Webhook endpoint? I have some simple thus far, but am really unclear of how to tackle this:


$json = file_get_contents('');
$json_decoded = json_decode($json, true);


I have tried sending multiple emails to the host, but every time I make this call I return

Answer Source

Try using the code from this example. This will give you the raw HTTP request body bytes to decode.

$data = file_get_contents("php://input");
$events = json_decode($data, true);

foreach ($events as $event) {
  // Here, you now have each event and can process them how you like

More info on php://input

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download