我想将接收到的 JSON 数据发送到我在服务器上编写的 PHP CURL 脚本,然后将数据转发到多个外部 webhook URL,条件是在特定字段键/值中接收到的数据。
但是,要么我的 IF 语句配置错误,要么我没有正确访问字段数据,因为我的测试 webhook 端点没有被传递到。如果我删除 IF 语句,代码会按预期传递数据。
$dataReceive = file_get_contents("php://input");
$dataEncode = json_encode($dataReceive, true);
$headers = array ( 'Content-type: application/json');
print_r($dataEncode);
$curl = curl_init();
curl_setopt( $curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt( $curl, CURLOPT_CUSTOMREQUEST, 'POST');
curl_setopt( $curl, CURLOPT_POST, 1);
curl_setopt( $curl, CURLOPT_POSTFIELDS, $dataEncode);
curl_setopt( $curl, CURLOPT_HTTPHEADER, $headers);
if ( $dataEncode ['Field 3'] == 'Test' ) {
curl_setopt( $curl, CURLOPT_URL, 'http://webhook1.com');
}
if ($dataEncode ['Field 3'] == 'Test Value 2' ) {
curl_setopt( $curl, CURLOPT_URL, 'http://webhook2.com');
}
$results = curl_exec($curl);
echo $results;
curl_close($curl);
$dataReceive 对象中接收到的 JSON 数据为:
{
"Timestamp": 1568838624687,
"Object": "Project",
"UserId": "",
"ObjectId__PhaseId": 111,
"Other__PhaseName": "Turndown",
"ProjectId": 111,
"OrgId": 111,
"Event": "PhaseChanged",
"ObjectId__ProjectTypeId": 1409
}
我正在通过使用 Postman 将虚拟数据发送到我的 PHP 脚本进行测试,但是一旦按预期工作,我实际上将过滤键“Other_PhaseName”。
一只斗牛犬
动漫人物