PHP form to Zapier: Best method for webhook POST requests?

Hey everyone, I’m struggling with sending POST requests from my PHP form to a Zapier webhook. I’ve tried a solution that worked before, but now it’s not doing the job. I’m using cURL to send the data, but something’s off. Here’s a simplified version of what I’m trying:

$url = 'https://hooks.zapier.com/hooks/catch/123456/abcdef/';
$data = array('name' => 'John Doe', 'email' => '[email protected]');

$ch = curl_init($url);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

$response = curl_exec($ch);
curl_close($ch);

Any ideas what might be going wrong? Or is there a better way to handle this? Thanks for any help!

I’ve dealt with similar webhook issues, and one thing that’s often overlooked is SSL certificate validation. By default, cURL verifies SSL certificates, which can cause problems if the certificate isn’t properly set up. Try adding this line to your cURL options:

curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);

This disables SSL verification, which might resolve your issue. However, be cautious with this in production environments as it can pose security risks.

Another thing to consider is the response from Zapier. Are you getting any response at all? If not, you might want to add some logging:

$response = curl_exec($ch);
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
error_log('Zapier response code: ’ . $httpCode);
error_log('Zapier response: ’ . $response);

This will help you see what’s happening on Zapier’s end. Sometimes, the issue isn’t with your code, but with how Zapier is processing the incoming data. Hope this helps troubleshoot your problem!

hey mate, have u tried using file_get_contents instead? It’s simpler and might work better. Something like:

$data = json_encode([‘name’ => ‘John’, ‘email’ => ‘[email protected]’]);
$options = [‘http’ => [
‘method’ => ‘POST’,
‘header’ => ‘Content-type: application/json’,
‘content’ => $data
]];
$context = stream_context_create($options);
$result = file_get_contents(‘your_webhook_url’, false, $context);

give it a shot and let us kno if it helps!

I’ve encountered similar issues before. One thing that often gets overlooked is error handling. Have you tried adding error checking to your cURL request? Something like this might help:

$response = curl_exec($ch);
if(curl_errno($ch)){
    echo 'Curl error: ' . curl_error($ch);
}

This will at least tell you if there’s a problem with the cURL request itself. Also, double-check your Zapier webhook URL and make sure it’s still active. Zapier sometimes deactivates webhooks if they haven’t been used in a while.

Lastly, verify that your data structure matches what Zapier is expecting. Sometimes, slight mismatches in the JSON structure can cause issues. Hope this helps!