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

12,718

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

<?php
$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
  process_event($event);
}
?>

More info on php://input

Share:
12,718
Jodo1992
Author by

Jodo1992

I am a 23-year-old Computer Science student at the University of Rhode Island and will graduate with a Bachelor of Science in Fall '16. I am experienced with object-oriented and imperative programming, a current rookie of functional and logical programming, and am always continually learning in all fields. I am very interested in security systems as well as video games. Not sure which field I will persue as a career but I would like to explore both options. On the side I am a classically trained singer and am a part of several on and off-campus choirs. In my free time I'm either coding, gaming, or watching Dr. Who.

Updated on June 05, 2022

Comments

  • Jodo1992
    Jodo1992 almost 2 years

    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:

    <?php
    
    $json = file_get_contents('https://example.com/webhook.php');
    $json_decoded = json_decode($json, true);
    $var_dump(json_decoded);
    
    ?>
    

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

    • Admin
      Admin over 7 years
      try using Guzzle in order to do the http request
    • Jodo1992
      Jodo1992 over 7 years
      Is that a service to debug webhooks?
  • Jodo1992
    Jodo1992 over 7 years
    Where would I call the url that SendGrid is POSTing to?
  • bwest
    bwest over 7 years
    You don't. that code goes into the file that your webhook URL points to... you then need to have that page hosted and available. You are creating a server to respond to requests. This post might help: sendgrid.com/blog/whats-webhook