POSTing cURL to Zapier Webhook

梦想与她 提交于 2020-01-24 10:24:05

问题


I'm trying to POST using cURL to a Zapier webhook.

Zapier is configured so that if I were to type their URL like so -- https://zapier.com/hooks/catch/n/abcd?email=foo@bar.com&guid=foobar

It will receive the post, but when I try to do the same thing with cURL, it does not seem to receive it.

Here's my code for posting with cURL -->

<?php
    // Initialize curl
    $curl = curl_init();

    // Configure curl options
    $opts = array(
        CURLOPT_URL             => 'https://zapier.com/hooks/catch/n/abcd',
        CURLOPT_RETURNTRANSFER  => true,
        CURLOPT_CUSTOMREQUEST   => 'POST',
        CURLOPT_POST            => 1,
        CURLOPT_POSTFIELDS      => 'guid='+ $_POST["guid"] + '&video_title=' + $_POST["video_title"] + '&email=' + $_POST["email"], 
    );

    // Set curl options
    curl_setopt_array($curl, $opts);

    // Get the results
    $result = curl_exec($curl);

    // Close resource
    curl_close($curl);

    echo $result;
?>

When I run this, it displays success, but Zapier does not receive it.

On Zapier's docs, someone gave an example with a proper cURL post, like so -->

curl -v -H "Accept: application/json" \
        -H "Content-type: application/json" \
        -X POST \
        -d '{"first_name":"Bryan","last_name":"Helmig","age":27}' \
        https://zapier.com/hooks/catch/n/Lx2RH/

I'm guessing that I'm missing something in the PHP file, help greatly appreciated!


回答1:


You need to json encode the data you are sending and set the content-type also:

Change:

$opts = array(
    CURLOPT_URL             => 'https://zapier.com/hooks/catch/n/abcd',
    CURLOPT_RETURNTRANSFER  => true,
    CURLOPT_CUSTOMREQUEST   => 'POST',
    CURLOPT_POST            => 1,
    CURLOPT_POSTFIELDS      => 'guid='+ $_POST["guid"] + '&video_title=' + $_POST["video_title"] + '&email=' + $_POST["email"], 
);

to:

$data = array('guid' => $_POST["guid"], 'video_title' => $_POST["video_title"], 'email' => $_POST["email"]);
$jsonEncodedData = json_encode($data);
$opts = array(
    CURLOPT_URL             => 'https://zapier.com/hooks/catch/n/abcd',
    CURLOPT_RETURNTRANSFER  => true,
    CURLOPT_CUSTOMREQUEST   => 'POST',
    CURLOPT_POST            => 1,
    CURLOPT_POSTFIELDS      => $jsonEncodedData,
    CURLOPT_HTTPHEADER  => array('Content-Type: application/json','Content-Length: ' . strlen($jsonEncodedData))                                                                       
);

This should work.




回答2:


Your not sending the POSTFIELDS correctly, you need to use . not + and also you should be url encoding the string...

$opts = array(
    CURLOPT_URL             => 'https://zapier.com/hooks/catch/n/abcd',
    CURLOPT_HEADER          => false,
    CURLOPT_RETURNTRANSFER  => true,
    CURLOPT_POST            => true,
    CURLOPT_POSTFIELDS      => http_build_query(array('guid' => $_POST['guid'], 'video_title' => $_POST['video_title'], 'email' => $_POST['email']))
);



回答3:


You don't receive it in Zapier because you haven't set the 'Child key' structure. Take a look at what you need to do inside the image below.

Keep in mind that in my case I wanted to catch the 'company_name'. You'll have to replace it with your own parameter. You can also define additional parameters or even completely change the 'Child key' structure.



来源:https://stackoverflow.com/questions/18483158/posting-curl-to-zapier-webhook

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!