Skip to main content

I have code for a webhook pull on my website. Here is the code:

 

$curl = curl_init();

curl_setopt_array($curl, array(
CURLOPT_URL => '
https://hooks.zapier.com/hooks/catch/8750787/2huuqpb/',
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => '',
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 0,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => 'POST',
CURLOPT_POSTFIELDS => json_encode(array(
array(
"Attribute" => "EmailAddress",
"Value" => $emailid
),
array(
"Attribute" => "FirstName",
"Value" => $firstname
),
array(
"Attribute" => "LastName",
"Value" => $lastname
),
array(
"Attribute" => "Phone",
"Value" => $mobileno
),
array(
"Attribute" => "mx_Best_Time_to_Call",
"Value" => $timetocall
),
array(
"Attribute" => "Source",
"Value" => "Website"
),
array(
"Attribute" => "Debt Value",
"Value" => $currentdebtlevel
),
array(
"Attribute" => "Debt Category",
"Value" => $debt_product
),
array(
"Attribute" => "Source of Income",
"Value" => $sourceofincome
),
array(
"Attribute" => "Payment Status",
"Value" => $payment_status
)
)),
CURLOPT_HTTPHEADER => array(
'Content-Type: application/json'
),
));

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

// Output response or handle it as needed
echo $response;

When i use the catch hook - it will only find 1 attribute and 1 value, and it splits up the requests. 

 

In this example it has put firstname as request H, lastname as request I and emailid as request J. It doesn’t pick up any other attributes. 

I tried switching to “catch raw hook” and all of the data goes into “raw body”. So i tried using “code by zapier” to parse it out via javascript:

 

var obj = JSON.parse(inputData.payload);

return obj;

When i test this, it again only returns the first attribute emailid. 

 

Please help! I feel like this should be easy. 

Hi @jmh600cbr 

Perhaps try asking ChatGPT for help troubleshooting your custom code.


Hi @jmh600cbr 

Perhaps try asking ChatGPT for help troubleshooting your custom code.

i did that. 

 

<?php
// Define your variables here
$emailid = 'example@example.com';
$firstname = 'John';
$lastname = 'Doe';
$mobileno = '1234567890';
$timetocall = 'Afternoon';
$currentdebtlevel = 'High';
$debt_product = 'Credit Card';
$sourceofincome = 'Employment';
$payment_status = 'Pending';

// Initialize cURL
$curl = curl_init();

// Set cURL options
curl_setopt_array($curl, array(
CURLOPT_URL => 'https://hooks.zapier.com/hooks/catch/8750787/2huuqpb/',
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => '',
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 0,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => 'POST',
CURLOPT_POSTFIELDS => json_encode(array(
array("Attribute" => "EmailAddress", "Value" => $emailid),
array("Attribute" => "FirstName", "Value" => $firstname),
array("Attribute" => "LastName", "Value" => $lastname),
array("Attribute" => "Phone", "Value" => $mobileno),
array("Attribute" => "mx_Best_Time_to_Call", "Value" => $timetocall),
array("Attribute" => "Source", "Value" => "Website"),
array("Attribute" => "Debt Value", "Value" => $currentdebtlevel),
array("Attribute" => "Debt Category", "Value" => $debt_product),
array("Attribute" => "Source of Income", "Value" => $sourceofincome),
array("Attribute" => "Payment Status", "Value" => $payment_status)
)),
CURLOPT_HTTPHEADER => array(
'Content-Type: application/json'
),
));

// Execute cURL request and check for errors
$response = curl_exec($curl);
if (curl_errno($curl)) {
echo 'cURL Error: ' . curl_error($curl);
} else {
// Output response or handle it as needed
echo $response;
}

// Close cURL session
curl_close($curl);
?>

still doesn’t work


@jmh600cbr 

Feedback from ChatGPT:

The issue likely arises because the `CURLOPT_POSTFIELDS` is sending a payload formatted as an array of objects, where each field is structured as an individual attribute-value pair. When Zapier receives this, it interprets each object as a separate payload item, which can result in separate data entries for each field.

Zapier webhooks generally expect a flat JSON object, where each field is sent as a key-value pair, not as an array of objects.

To fix this issue, you should modify the `CURLOPT_POSTFIELDS` section to send the data as a flat key-value structure. Here's how you can adjust the code:

<?php
// Define your variables here
$emailid = 'example@example.com';
$firstname = 'John';
$lastname = 'Doe';
$mobileno = '1234567890';
$timetocall = 'Afternoon';
$currentdebtlevel = 'High';
$debt_product = 'Credit Card';
$sourceofincome = 'Employment';
$payment_status = 'Pending';

// Initialize cURL
$curl = curl_init();

// Set cURL options
curl_setopt_array($curl, array(
CURLOPT_URL => 'https://hooks.zapier.com/hooks/catch/000/XXX/',
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => '',
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 0,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => 'POST',
CURLOPT_POSTFIELDS => json_encode(array(
"EmailAddress" => $emailid,
"FirstName" => $firstname,
"LastName" => $lastname,
"Phone" => $mobileno,
"mx_Best_Time_to_Call" => $timetocall,
"Source" => "Website",
"Debt_Value" => $currentdebtlevel,
"Debt_Category" => $debt_product,
"Source_of_Income" => $sourceofincome,
"Payment_Status" => $payment_status
)),
CURLOPT_HTTPHEADER => array(
'Content-Type: application/json'
),
));

// Execute cURL request and check for errors
$response = curl_exec($curl);
if (curl_errno($curl)) {
echo 'cURL Error: ' . curl_error($curl);
} else {
// Output response or handle it as needed
echo $response;
}

// Close cURL session
curl_close($curl);
?>

 

### Key changes:
- Instead of sending an array of objects (`array()`), the data is now structured as a flat key-value pair, which is what Zapier expects in most webhook POST requests.
 
By sending the data this way, Zapier will receive a single payload with the fields as distinct elements in a single object, avoiding the issue of separate entries for each field.