PHP Webhook - x-www-form-urlencode parse



我正在尝试建立一个kofi webhook解析器,它发送的数据是这样的

data: {
"message_id":"3a1fac0c-f960-4506-a60e-824979a74e74",
"kofi_transaction_id":"0a1fac0c-f960-4506-a60e-824979a74e71",
"timestamp":"2022-08-21T13:04:30Z",
"type":"Donation",
"from_name":"Ko-fi Team",
"message":"Good luck with the integration!",
"amount":"3.00",
"currency":"USD",
"url":"https://ko-fi.com"
"is_subscription_payment":false
"is_first_subscription_payment":false}

发送的数据内容类型为application/x-www-form-urlencoded。一个名为'data'的字段以JSON字符串的形式包含支付信息(根据网站信息)。

我有一个php脚本来尝试获取数据

parse_str(file_get_contents("php://input"), $data);
//$data = (object)$data;
$data = json_decode(json_encode($data), true);
var_dump($data);
error_log($data->{'message_id'});

我似乎无法获得message_id的值

邮差的回报

array(12) {
["data"]=>
string(2) " {"
[""message_id""]=>
string(39) ""3a1fac0c-f960-4506-a60e-824979a74e74","
[""kofi_transaction_id""]=>
string(39) ""0a1fac0c-f960-4506-a60e-824979a74e71","
[""timestamp""]=>
string(23) ""2022-08-21T13:04:30Z","
[""type""]=>
string(11) ""Donation","
[""from_name""]=>
string(13) ""Ko-fi Team","
[""message""]=>
string(34) ""Good luck with the integration!","
[""amount""]=>
string(7) ""3.00","
[""currency""]=>
string(6) ""USD","
[""url""]=>
string(19) ""https://ko-fi.com""
[""is_subscription_payment""]=>
string(5) "false"
[""is_first_subscription_payment""]=>
string(6) "false}"
}

我不相信邮递员发送的数据是准确的,但它的格式很奇怪。

请有人帮我,我做错了什么?我基本上想要接收数据,并从接收到的数据中提取值,我将把这些值推送到数据库中。

Ko-fi发送urlencoded表单数据,这意味着您必须使用urldecode来解码传入字符串,然后才能解析它并解码json对象。

parse_str(urldecode(file_get_contents("php://input")), $input);
$webhook = json_decode($input['data']);
var_dump($webhook->message_id);

你当然明白你不能查看传入的数据-你需要把它保存在某个地方…在下一个例子中,我将收到的json保存到一个txt文件中,以查看是否甚至被接收。

试着这样做:

$webhookContent = '';
$webhook = fopen("php://input" , "rb");
while (!feof($webhook)) {
$webhookContent .= fread($webhook, 4096);
}
fclose($webhook);
error_log($webhookContent);
$webhookContent = str_replace("'", "", $webhookContent);
$WebHookArray = json_decode($webhookContent,true);
$newFile = fopen("someFile.txt","w");
fwrite($newFile,$webhookContent . "n");
fclose($newFile);

这将把数据保存到本地文件(如果它没有创建-创建它,只是为了测试给它777权限,这样它就会在那里保存数据....)

获取json并在线解码或通过php json_decode($json,true);<-别忘了,真实的……看看它是否变成了一个标准数组你可以获取&;message_id&;从.

如果一切似乎都是正确的…以json格式…$WebHookArray['message_id'] - json_decoded already.

我试过Kim Hallberg的代码。虽然它可以很好地处理webhook测试消息,但它无法处理真正的Ko-fi捐赠,因为"url"中有一个&号。参数,导致使用parse_str解析数据时出错。

我最终写了我自己的代码,工作得很好,虽然我用substr函数摆脱了最初的data=部分:

// Get the input, remove the initial "data=" part, decode the url, and convert to a JSON object
$data = json_decode(urldecode(substr(file_get_contents("php://input"), 5)));
// Check the verification code, replace the verification code with your own!
if ($data->verification_token != '9a1121c4-bd1c-11ed-afa1-0242ac120002') {
// If the verification token is not valid, set the response code to 403 Forbidden, and terminate the code execution.
http_response_code(403);
exit(1);
}
// Do what you need to do with the data
// Example: dump $data to file
ob_start();
var_dump($data);
file_put_contents('kofi_data_dump.txt', ob_get_clean());

最新更新