向Zapier Webhook发送POST请求的cURL

3
我正在尝试使用cURL向Zapier webhook发送POST请求。
Zapier已经配置好了,如果我像这样输入它们的URL -- https://zapier.com/hooks/catch/n/abcd?email=foo@bar.com&guid=foobar 它将会接收到POST请求,但是当我尝试使用cURL做同样的事情时,它似乎没有接收到。
这是我使用cURL进行POST请求的代码 -->
<?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 post 的示例,如下所示 -->
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 文件中我漏掉了什么,非常感谢您的帮助!

4个回答

7
您需要对发送的数据进行json编码,并设置content-type:

更改为:

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

这应该可以正常工作。


今天这个答案还有效吗?我也遇到了这个问题。 - StephenB

1
你没有正确发送 POSTFIELDS,需要使用 . 而不是 +,同时你应该对字符串进行 URL 编码...
$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']))
);

0
为了提供一些功能,从之前的代码中提取出来并制作了一个简单的可重复使用的函数。
function send_array_to_zapier_webhook($php_array, $hook_url){
  // Initialize curl
  $curl = curl_init();

  $json_encoded_data = json_encode($php_array);
  $opts = array(
    CURLOPT_URL => $hook_url,
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_CUSTOMREQUEST => 'POST',
    CURLOPT_POST => 1,
    CURLOPT_POSTFIELDS => $json_encoded_data,
    CURLOPT_HTTPHEADER => array('Content-Type: application/json','Content-Length: ' . strlen($json_encoded_data))
  );

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

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

  // Close resource
  curl_close($curl);
}

0

在 Zapier 中没有收到它,因为您还没有设置“子键”结构。请查看下面的图像以了解需要执行的操作。

请记住,在我的情况下,我想捕获“company_name”。您将不得不用自己的参数替换它。您还可以定义其他参数,甚至完全更改“子键”结构。

enter image description here


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接