将cURL发布到Zapier Webhook

时间:2013-08-28 08:40:49

标签: php curl webhooks zapier

我正在尝试使用cURL发布到Zapier webhook。

配置Zapier,如果我这样输入他们的网址 - https://zapier.com/hooks/catch/n/abcd?email=foo@bar.com&guid=foobar

它会收到帖子,但是当我尝试用cURL做同样的事情时,它似乎没有收到它。

这是我用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;
?>

当我运行它时,它显示成功,但Zapier没有收到它。

在Zapier的文档中,有人给出了一个适当的cURL帖子的例子,就像这样 - &gt;

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/

我猜我错过了PHP文件中的内容,非常感谢!

3 个答案:

答案 0 :(得分:3)

您需要对要发送的数据进行json编码并设置内容类型:

变化:

$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"], 
);

为:

$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))                                                                       
);

这应该有用。

答案 1 :(得分:0)

您没有正确发送POSTFIELDS,您需要使用.而不是+,而且您应该对字符串进行网址编码...

$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']))
);

答案 2 :(得分:0)

您尚未在Zapier中收到它,因为尚未设置“子键”结构。 看看下面的图片中需要做什么。

请记住,就我而言,我想捕捉'company_name'。您必须将其替换为自己的参数。您还可以定义其他参数,甚至可以完全更改“子键”的结构。

enter image description here

相关问题