处理webhook json输入

时间:2015-04-14 13:44:55

标签: php json

我使用此代码来跟踪webhook(当前它保存了一个文件,以后我想直接处理数据,然后将有用的数据保存在数据库中):

<?php

ob_start();
var_dump(file_get_contents('php://input'));
$a = ob_get_contents();
ob_end_clean();

file_put_contents(dirname(__FILE__).DIRECTORY_SEPARATOR.'log.txt', $b, FILE_APPEND);

这是一个例子,文件中写的是什么:

string(1387) "{"action":{"id":"1234","idMemberCreator":"1234","data":{"list":{"name":"A third list","id":"1234"},"board":{"shortLink":"BcD","name":"Test Board","id":"1234"},"card":{"shortLink":"HfT","idShort":54,"name":"Test It","id":"1234","pos":108576},"old":{"pos":4952}},"type":"updateCard","date":"2015-04-14T13:24:51.759Z","memberCreator":{"id":"1234","avatarHash":"1234","fullName":"Peter","initials":"p","username":"peter"}},"model":{"id":"1234","name":"Test Board","desc":"","descData":null,"closed":false,"idOrganization":null,"pinned":false,"url":"http://test.de","shortUrl":"http://test.de","prefs":{"permissionLevel":"public","voting":"disabled","comments":"members","invitations":"members","selfJoin":false,"cardCovers":true,"cardAging":"regular","calendarFeedEnabled":false,"background":"blue","backgroundColor":"#0079BF","backgroundImage":null,"backgroundImageScaled":null,"backgroundTile":false,"backgroundBrightness":"unknown","canBePublic":true,"canBeOrg":true,"canBePrivate":true,"canInvite":true},"labelNames":{"green":"","yellow":"","orange":"","red":"","purple":"","blue":"","sky":"","lime":"","pink":"","black":""}}}"

(出于安全目的,我将所有ID更改为1234)

我该怎么做?获取操作的ID或更难:用户的用户名

我不知道如何处理“string(1387)......”

这是输入的json_decode(我手动完成,但我想用php做): http://codebeautify.org/jsonviewer/2aab86

有人可以帮我,获取这些信息吗?

谢谢

1 个答案:

答案 0 :(得分:0)

从我看到你使用Trello Webhook。他们的请求jeson不会自动处理到$_POST

因此你需要手工处理jason-daten:

$json = file_get_contents('php://input')
$action = json_decode($json, true);

你可以像普通的json datan一样使用它:

$action['action']['data']...

使用它来查看数组的外观:

echo "<pre>print_r($action,true)</pre>";