How do I handle webhooks in PHP?

Handling webhooks in PHP is a straightforward process that involves setting up an endpoint to receive HTTP POST requests from a service. Below is an example of how to implement a simple webhook listener in PHP.

<?php // Set the content type to JSON header('Content-Type: application/json'); // Retrieve the incoming request body $json = file_get_contents('php://input'); // Decode the JSON data into a PHP array $data = json_decode($json, true); // Log or process the data as needed if (!empty($data)) { // Example: Log the webhook data to a file file_put_contents('webhook.log', print_r($data, true), FILE_APPEND); // Here, you can implement your logic based on the received data } // Respond back with a success message echo json_encode(['status' => 'success']); ?>

webhooks PHP webhook handling PHP webhook example receiving webhooks in PHP processing webhook data