简体   繁体   中英

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.

$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.

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.

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"];
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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