简体   繁体   English

如何通过Webhook获取数据? (GoCardless)

[英]How to get data via Webhook? (GoCardless)

I'm testing using Webhooks for the first time. 我是第一次使用Webhooks进行测试。 I want to set up a Webhook (using GoCardless.com, but this shouldn't matter now). 我想设置一个Webhook(使用GoCardless.com,但这现在不重要了)。

I'm in the sandbox and I have set up a Webhook there: 我在沙盒中,并在其中设置了Webhook:

{
  "id": "WE000012RYRQ9E",
  "created_at": "2017-03-01T18:03:34.252Z",
  "enabled": true,
  "secret": "qpILcgnnAWznkuJcajXYvMNsQcrkKwDzL6KHMq5z",
  "name": "TestHook",
  "url": "https://example.com/webhook.php",
  "client_cert_details": null,
  "links": {}
}

Then I'm trying to send a Webhook to my webhook.php having this code: 然后,我尝试将Webhook发送到我的webhook.php并包含以下代码:

 <?php
header('Content-Type: application/json');

$result = $_REQUEST['events'];
$obj = json_decode($result, true);

print_r($_GET);
print_r($_POST);
print_r($_REQUEST);
print_r($result);
print_r($obj);

You can see the result here: 您可以在此处查看结果:

在此处输入图片说明

So, I assumed that I'll get the data with $_REQUEST['events'] like displayed, but at the bottom you can see that I only get empty arrays back. 因此,我假设我将使用显示的$_REQUEST['events']获取数据,但是在底部,您可以看到我只得到了空数组。

Is this something about the secret key ( qpILcgnnAWznkuJcajXYvMNsQcrkKwDzL6KHMq5z )? 这与密钥( qpILcgnnAWznkuJcajXYvMNsQcrkKwDzL6KHMq5z )有关吗?

I don't know how to set this up. 我不知道该如何设置。

The webhook data is sent to your server as JSON in the body of the request. Webhook数据在请求正文中作为JSON发送到您的服务器。 In PHP, you'll need to access this with file_get_contents('php://input') . 在PHP中,您需要使用file_get_contents('php://input')

Once you've got the body, you can decode it into an array, and then work with the data however you want: 获得主体后,可以将其解码为数组,然后根据需要使用数据:

$raw_payload = file_get_contents('php://input');
$payload = json_decode($raw_payload, true);

// Each webhook may contain multiple events to handle, batched together
foreach ($payload["events"] as $event) {
    // Do something with the event here
    print("Processing event " . $event["id"] . "\n");
}

For a full guide on handling webhooks, see GoCardless's getting started guide . 有关处理Webhook的完整指南,请参阅GoCardless 入门指南 That also shows you how to use the secret key to make sure a webhook really comes from GoCardless, and isn't being spoofed. 这还向您展示了如何使用密钥来确保Webhook确实来自GoCardless,并且不会被欺骗。

You've included your webhook secret in this post, so you'll want to reset that now, otherwise someone could send you fake webhooks pretending to be GoCardless. 您已在这篇文章中添加了Webhook机密,因此,您现在就想重新设置该机密,否则有人会向您发送假冒GoCardless的假Webhooks。 You can do that in your sandbox GoCardless dashboard by clicking "Developer" on the left-hand side, then clicking on your endpoint, then clicking "Edit". 您可以在沙箱GoCardless仪表板中执行此操作,方法是单击左侧的“开发人员”,然后单击端点,然后单击“编辑”。

I just found an example after searching a lot. 经过大量搜索,我才找到一个示例。 Maybe this helps someone else. 也许这可以帮助别人。 Note that most code seems to be deprecated but you'll know that when you've studied the uptodate GoCardless API : 请注意,大多数代码似乎已被弃用,但是当您学习最新的GoCardless API时,您会知道:

https://github.com/gocardless/gocardless-legacy-php/blob/master/examples/webhooks.php https://github.com/gocardless/gocardless-legacy-php/blob/master/examples/webhooks.php

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM