Reputation: 59
so I am trying to receive JSON data from one webhook, use PHP to filter for some conditions, and then send the data to an external webhook address based on those conditions.
So for example, I created a php file on my server called "webhook.php":
$dataReceive = file_get_contents("php://input");
$dataEncode = json_encode($dataReceive, true);
print_r($dataEncode);
$curl = curl_init();
$opts = array (
CURLOPT_URL => 'https://hooks.zapier.com/hooks/catch/',
CURLOPT_RETURNTRANSFER => TRUE,
CURLOPT_CUSTOMREQUEST => 'POST',
CURLOPT_POST => 1,
CURLOPT_POSTFIELDS => $dataEncode,
CURLOPT_HTTPHEADER => array (
'Content-type: application/json'
)
);
curl_setopt($curl, $opts);
$results = curl_exec($curl);
echo $results;
curl_close($curl);
The "php://input" can either be exactly as it is, or I tried replacing it with the URL of my webhook.php file just in case. I can test my webhook using Postman, and I am returned a 200 OK, but the data is never sent to my external webhook (https://hooks.zapier.com/hooks/catch/).
I have written the conditional PHP code yet; I just want to ensure I can send and receive this data properly first. Any guidance is much appreciated!
Upvotes: 1
Views: 3838
Reputation: 64
Problem is with curl_setopt. You need to pass three argument for this method curl_setopt ( resource $ch , int $option , mixed $value ). You can set these by following
$curl = curl_init();
$opts = array (
CURLOPT_URL => 'https://hooks.zapier.com/hooks/catch/',
CURLOPT_RETURNTRANSFER => TRUE,
CURLOPT_CUSTOMREQUEST => 'POST',
CURLOPT_POST => 1,
CURLOPT_POSTFIELDS => $dataEncode,
CURLOPT_HTTPHEADER => array (
'Content-type: application/json'
)
);
foreach ($opts as $key => $value) {
curl_setopt($curl, $key, $value);
}
$results = curl_exec($curl);
echo $results;
curl_close($curl);
Or you can set them individually like this
curl_setopt($curl, CURLOPT_URL, 'https://hooks.zapier.com/hooks/catch/');
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
.....
Upvotes: 1