接收webhook数据并保存在db中
Receiving webhook data and save them in db
我想处理由 trello webhook 发送的数据。
posts 到 url 之类的网站。com/tracker.php
在tracker.php我想把数据保存在数据库中。为此,我需要获取一些参数。
这是我收到的代码示例 (https://trello.com/docs/gettingstarted/webhooks.html):
{
"action": {
"id":"51f9424bcd6e040f3c002412",
"idMemberCreator":"4fc78a59a885233f4b349bd9",
"data": {
"board": {
"name":"Trello Development",
"id":"4d5ea62fd76aa1136000000c"
},
"card": {
"idShort":1458,
"name":"Webhooks",
"id":"51a79e72dbb7e23c7c003778"
},
"voted":true
},
"type":"voteOnCard",
"date":"2013-07-31T16:58:51.949Z",
"memberCreator": {
"id":"4fc78a59a885233f4b349bd9",
"avatarHash":"2da34d23b5f1ac1a20e2a01157bfa9fe",
"fullName":"Doug Patti",
"initials":"DP",
"username":"doug"
}
},
"model": {
"id":"4d5ea62fd76aa1136000000c",
"name":"Trello Development",
"desc":"Trello board used by the Trello team to track work on Trello. How meta!\n\nThe development of the Trello API is being tracked at https://trello.com/api\n\nThe development of Trello Mobile applications is being tracked at https://trello.com/mobile",
"closed":false,
"idOrganization":"4e1452614e4b8698470000e0",
"pinned":true,
"url":"https://trello.com/b/nC8QJJoZ/trello-development",
"prefs": {
"permissionLevel":"public",
"voting":"public",
"comments":"public",
"invitations":"members",
"selfJoin":false,
"cardCovers":true,
"canBePublic":false,
"canBeOrg":false,
"canBePrivate":false,
"canInvite":true
},
"labelNames": {
"yellow":"Infrastructure",
"red":"Bug",
"purple":"Repro'd",
"orange":"Feature",
"green":"Mobile",
"blue":"Verified"
}
}
}
这是我当前的 tracker.php 文件:
<?php
$json = $_POST["actions"];
$action = json_decode($json);
$action_id = $action->id;
$card_id = $action->data->card->id;
var_dump($array);
我的问题:
- $_POST["actions"] 对吗?或者我在 []
里面需要什么
- 我想获取$action->data->card->id的方式对吗?
- 有什么方法可以查看 var_dump 的结果吗?不知道如何查看 webhook 的结果 post..
我不得不使用这个:
$json = file_get_contents('php://input');
$action = json_decode($json, true);
据我了解,json 请求不会自动拆分为 $_POST。因此,您必须使用输入本身。
需要 json_decode 中的真参数来获取关联数组。没有它我只有一个空数组。
您可以使用它来检查接收到的数据是否为 JSON 格式。
if($json = json_decode(file_get_contents("php://input"), true)) {
print_r($json);
$data = $json;
} else {
print_r($_POST);
$data = $_POST;
}
我想处理由 trello webhook 发送的数据。 posts 到 url 之类的网站。com/tracker.php
在tracker.php我想把数据保存在数据库中。为此,我需要获取一些参数。
这是我收到的代码示例 (https://trello.com/docs/gettingstarted/webhooks.html):
{
"action": {
"id":"51f9424bcd6e040f3c002412",
"idMemberCreator":"4fc78a59a885233f4b349bd9",
"data": {
"board": {
"name":"Trello Development",
"id":"4d5ea62fd76aa1136000000c"
},
"card": {
"idShort":1458,
"name":"Webhooks",
"id":"51a79e72dbb7e23c7c003778"
},
"voted":true
},
"type":"voteOnCard",
"date":"2013-07-31T16:58:51.949Z",
"memberCreator": {
"id":"4fc78a59a885233f4b349bd9",
"avatarHash":"2da34d23b5f1ac1a20e2a01157bfa9fe",
"fullName":"Doug Patti",
"initials":"DP",
"username":"doug"
}
},
"model": {
"id":"4d5ea62fd76aa1136000000c",
"name":"Trello Development",
"desc":"Trello board used by the Trello team to track work on Trello. How meta!\n\nThe development of the Trello API is being tracked at https://trello.com/api\n\nThe development of Trello Mobile applications is being tracked at https://trello.com/mobile",
"closed":false,
"idOrganization":"4e1452614e4b8698470000e0",
"pinned":true,
"url":"https://trello.com/b/nC8QJJoZ/trello-development",
"prefs": {
"permissionLevel":"public",
"voting":"public",
"comments":"public",
"invitations":"members",
"selfJoin":false,
"cardCovers":true,
"canBePublic":false,
"canBeOrg":false,
"canBePrivate":false,
"canInvite":true
},
"labelNames": {
"yellow":"Infrastructure",
"red":"Bug",
"purple":"Repro'd",
"orange":"Feature",
"green":"Mobile",
"blue":"Verified"
}
}
}
这是我当前的 tracker.php 文件:
<?php
$json = $_POST["actions"];
$action = json_decode($json);
$action_id = $action->id;
$card_id = $action->data->card->id;
var_dump($array);
我的问题:
- $_POST["actions"] 对吗?或者我在 [] 里面需要什么
- 我想获取$action->data->card->id的方式对吗?
- 有什么方法可以查看 var_dump 的结果吗?不知道如何查看 webhook 的结果 post..
我不得不使用这个:
$json = file_get_contents('php://input');
$action = json_decode($json, true);
据我了解,json 请求不会自动拆分为 $_POST。因此,您必须使用输入本身。
需要 json_decode 中的真参数来获取关联数组。没有它我只有一个空数组。
您可以使用它来检查接收到的数据是否为 JSON 格式。
if($json = json_decode(file_get_contents("php://input"), true)) {
print_r($json);
$data = $json;
} else {
print_r($_POST);
$data = $_POST;
}