2013-08-28 53 views
0

我想用cURL张贴到Zapier的webhook。张贴到Zapier Webhook的卷曲

Zapier配置,这样如果我输入自己的网址,像这样 - https://zapier.com/hooks/catch/n/[email protected]&guid=foobar

将收到帖子,但是当我尝试做卷曲同样的事情,它似乎并不接受它。

这里是我的代码与卷曲发布 - >

<?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 -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文件的东西,帮助非常感谢!

回答

2

你需要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'])) 
);