将cURL发布到Zapier Webhook

问题描述 投票:3回答:3

[我正在尝试使用cURL将其发布到Zapier Webhook。

Zapier已配置,因此,如果我这样输入他们的URL,https://zapier.com/hooks/catch/n/[email protected]&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帖子的示例,例如->

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文件中缺少某些内容,非常感谢您的帮助!

php curl webhooks zapier
3个回答
5
投票
您需要对要发送的数据进行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)) );

这应该起作用。

0
投票
您没有正确发送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
投票
您没有在Zapier中收到它,因为尚未设置'Child key'结构。在下面的图片中查看您需要执行的操作。

请记住,就我而言,我想抓住

'company_name'。您必须将其替换为自己的参数。您还可以定义其他参数,甚至可以完全更改“子键”的结构。

enter image description here
© www.soinside.com 2019 - 2024. All rights reserved.