rryys
rryys

Reputation: 209

How to get webhook response data

I'm still new to webhook. What I need to do here is to do a callback whenever there's a new registration on the registration platform called Bizzabo. This platform has provided Webhook integration by having us putting the Endpoint URL and select which action that will trigger the Webhook. I've also used Request Bin and it displays the data well.

However, how can I echo the JSON body data like how it displayed in Request Bin in my interface URL php?

This is how the Webhook integration looks like on Bizzabo

Data captured from Webhook when tested using Request Bin

Thank you!

Upvotes: 3

Views: 16308

Answers (2)

Dnyaneshwar Harer
Dnyaneshwar Harer

Reputation: 842

If the data receiving in compressed format(gzip) use gzdecode :

<?php

    if (!function_exists('gzdecode')){
        function gzdecode($data){
            // strip header and footer and inflate
            return gzinflate(substr($data, 10, -8));
        }
    }

    // get compressed (gzip) POST request into a string
    $comprReq = file_get_contents('php://input');

    // get decompressed POST request
    $decomprReq = gzdecode($comprReq);
     // decode to json
    $jsonData = json_decode($decomprReq, true);
    // do your processing on $jsonData
?>

Upvotes: 0

Lawrence Cherone
Lawrence Cherone

Reputation: 46602

Your need an endpoint which receives the callback instead Request Bin, then access it in the following way using file_get_contents('php://input') and json_decode()

For example http://example.com/bizzabo-callback-handler.php

<?php
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
    // fetch RAW input
    $json = file_get_contents('php://input');

    // decode json
    $object = json_decode($json);

    // expecting valid json
    if (json_last_error() !== JSON_ERROR_NONE) {
        die(header('HTTP/1.0 415 Unsupported Media Type'));
    }

    /**
     * Do something with object, structure will be like:
     * $object->accountId
     * $object->details->items[0]['contactName']
     */
    // dump to file so you can see
    file_put_contents('callback.test.txt', print_r($object, true));
}

Upvotes: 13

Related Questions