I’m struggling with implementing webhooks in PHP and could really use some guidance from experienced developers.
My company needs me to create an endpoint that another business can use to send JSON data to our system. I’m pretty new to webhooks so I’m not sure if I’m on the right track.
I found this code snippet online but I’m not sure if it’s what I need:
// Initialize cURL session
$curl_handle = curl_init();
// Skip SSL certificate verification
curl_setopt($curl_handle, CURLOPT_SSL_VERIFYPEER, false);
// Return response as string instead of outputting
curl_setopt($curl_handle, CURLOPT_RETURNTRANSFER, true);
// Set target URL
curl_setopt($curl_handle, CURLOPT_URL, $target_url);
// Execute request
$response = curl_exec($curl_handle);
// Close cURL session
curl_close($curl_handle);
// Display formatted JSON response
var_dump(json_decode($response, true));
My main question is: should the URL I provide to the external company point directly to this PHP script? I’m getting confused about how the whole process should work. Any help would be appreciated!
That code’s for sending HTTP requests, not receiving webhooks. I made the exact same mistake when I started working with inventory webhooks - spent hours wondering why nothing worked. Your webhook endpoint needs to just sit there and wait for incoming data. Create a PHP file that checks the request method, grabs the raw POST data with file_get_contents('php://input'), and processes it. Point the external company’s URL directly to this script. Here’s what I wish someone told me: always log incoming requests during development. You’ll see exactly what format the data comes in. Also check if your server can handle their request volume - we had timeout issues when processing large batches of notifications.
Yeah, that confusion makes total sense - you’re looking at outbound cURL when you need inbound webhook handling. I hit the same wall when I first started with payment webhooks. Your endpoint just needs to be a simple PHP script that handles incoming POST requests. Start basic: check if it’s a POST request, grab the raw input with file_get_contents('php://input'), and decode it with json_decode(). The URL you give them should point straight to this script. Learned this the hard way - always validate the incoming data structure before doing anything with it, and return proper HTTP status codes (200 for success, 400 for bad requests). Also worth adding some authentication to verify who’s actually sending the data.
You’ve got it backwards. That code sends requests out - webhooks receive data coming in. You need an endpoint that listens for POST requests. Make a PHP file that grabs the incoming JSON with $_POST or file_get_contents('php://input') for raw JSON. Validate the data, run your business logic, and send back the right HTTP status code. Give the external company the URL that points straight to this script. Don’t forget security - verify who’s sending the data with tokens or signatures before you process anything.
easiest way to test this? just create a simple php file that logs everything first. use error_log(file_get_contents('php://input')); to see what data’s actually coming in. build from there once u know the format works.
that code’s actually for making requests, not receiving them. for webhooks, u wanna use file_get_contents('php://input') to get the incoming json. also, make sure the url you give them goes to your receiving script, not the sending one!