[英]cURL POST request in PHP script using file_get_contents
I'm using a Zapier Webhook to connect a form to Salesforce.我正在使用 Zapier Webhook 将表单连接到 Salesforce。
I'm trying to get data from the form to POST to my Zap.我正在尝试从表单获取数据以 POST 到我的 Zap。
Before connecting to Zapier I had a script that was connected to a custom api and we used file_get_contents to get our inputs.在连接到 Zapier 之前,我有一个连接到自定义 api 的脚本,我们使用 file_get_contents 来获取我们的输入。 This script isn't working and isn't triggering the Zap.
此脚本不起作用,也没有触发 Zap。 I don't normally work in PHP so I'm not sure what the problem could be.
我通常不在 PHP 工作,所以我不确定问题可能是什么。
Here is the new script I'm working on utilizing Zapier:这是我正在使用 Zapier 编写的新脚本:
<?php
$json_data = file_get_contents('php://input');
$json = json_encode($json_data);
$curl = curl_init();
curl_setopt_array($curl, $json, array(
CURLOPT_URL => 'zapier webhook url',
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => '',
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 0,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => 'POST',
CURLOPT_POSTFIELDS => $json,
CURLOPT_HTTPHEADER => array(
'Content-Type: application/json'
),
));
$response = curl_exec($curl);
curl_close($curl);
echo $response;
Our old script utilizing a custom api (this one works):我们使用自定义 api 的旧脚本(这个有效):
<?php
header("Access-Control-Allow-Origin: *");
header("Access-Control-Allow-Methods: POST, GET, OPTIONS");
header("Access-Control-Allow-Headers: Content-Type, Authorization, X-Requested-With");
$json_data = file_get_contents('php://input');
$opts = array(
'http' => array(
'header' => "Content-type: application/json\r\n" . "ClientID: id#\r\n",
'method' => 'POST',
'content' => $json_data,
),
);
$context = stream_context_create($opts);
$url = 'custom api';
$response = file_get_contents($url, false, $context);
exit();
I tried putting Zapier Webhook into the old script and it didn't work either.我尝试将 Zapier Webhook 放入旧脚本中,但它也不起作用。 It doesn't seem to be an issue with our Zap because I can submit success Postman requests and I the new script is close to what Postman generated for me (with the exception of trying to get the data from file_get_contents instead of a hard coded object.)
这似乎不是我们的 Zap 的问题,因为我可以提交成功的 Postman 请求,并且我的新脚本接近为我生成的 Postman (除了尝试从 file_get_contents 获取数据而不是硬编码的 ZA666CFDE63198BC4BEB66 .)
You basically got it right.你基本上是对的。 The only error I can spot is that the JSON data object is being provided as the second argument of your curl_setopt_array() call.
我能发现的唯一错误是 JSON 数据 object 作为 curl_setopt_array() 调用的第二个参数提供。 That's not where it belongs.
那不属于它。
Just remove the $json data object, which is already getting submitted as a postfield within the cURL request.只需删除 $json 数据 object,它已经作为 cURL 请求中的 postfield 提交。
curl_setopt_array($curl, array(
...
));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.