[我正在尝试使用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文件中缺少某些内容,非常感谢您的帮助!
更改:
$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)) );
这应该起作用。
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']))
);