简体   繁体   English

如何从 webhook 获取数据?

[英]how to get the data from a webhook?

I am using a webhook from Laposta, it will be activated when a user unsubscribed from a newsletter.我正在使用 Laposta 的 webhook,当用户取消订阅时事通讯时,它将被激活。

$sJsonData = @file_get_contents('php://input');

mail('xxx@xx.com', 'webhook json',$sJsonData); // Email to myself to see what's within the webhook

// Decode JSON data to PHP associative array
$arr = json_decode($sJsonData, true);

// Access values from the associative array
$event1 = $arr["event"];
$event2 = $arr["data"]["event"];
$event3 = $arr["data"]["data"]["event"];

mail('xxx@xx.com', 'webhook', 'event1 = ' . $event1 . ' and event2 = ' . $event2  . ' and event3 = ' . $event3); // Email to myself to see what's the value of the variable $event1(2,3)

I get in the first email this:我收到第一封电子邮件:

{
"data": [
    {
        "type": "member",
        "event": "deactivated",
        "data": {
            "member_id": "***",
            "list_id": "***",
            "email": "xxx@xx.com",
            "state": "unsubscribed",
            "signup_date": "2020-11-18 15:50:34",
            "modified": "2020-11-23 16:56:25",
            "confirm_date": null,
            "ip": "***",
            "source_url": "",
            "custom_fields": {
                "spelersnaam": "***"
            }
        },
        "info": {
            "source": "external",
            "action": "unsubscribed",
            "date_event": "2020-11-23 17:05:15"
        }
    }
],
"date_requested": "2020-11-23 17:05:20"
}

But in my second mail, all the 3 variables are empty.但是在我的第二封邮件中,所有 3 个变量都是空的。

What am I doing wrong?我究竟做错了什么?

I want the data email, state and date_event in 3 variables so I can make a MySQL request to change this record in my database.我想要 3 个变量中的数据 email、state 和 date_event,以便我可以发出 MySQL 请求来更改我数据库中的这条记录。

Kind regards,亲切的问候,

Arie阿里

After some trial and error moments, I code this working code:经过一些反复试验,我编写了这个工作代码:

if (is_array($arr) && isset($arr["data"][0]["data"])) {
    $arrLength = count($arr["data"]);
    for ($i = 0; $i < $arrLength; $i++) {
        $Item = $arr["data"][$i];
        echo $Item["event"];
        echo $Item["data"]["email"];
    }
}

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

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