phpwebhooksurldecodex-www-form-urlencoded

How do I access values from urlencode webhook notification content


I am trying to get values and update my db with them once a notification is sent to my webhook. The content is urlendoced. When I save it as a txt file the content looks like this:

MsgId=fd1c39f0-e443-11ec-8460-00000a148c01&Status=2&StatusText=sent&DestinationAdress=%2B2347030202868&OriginatedAddress=&CreateDateTime=2022-06-04+22%3A22%3A00&SendDateTime=2022-06-04+22%3A22%3A00&DeliveryDateTime=&Reason=002347030202868%3A20220604222200&SmsCount=1&RSN=000&remoteid=2022082201465615

But trying to access each of the variables and getting the value is proving difficult.

Below is my code:

<?php
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
require_once 'dbcon/v20/dbcon.php';

$input = urldecode(file_get_contents("php://input"));
//file_put_contents("test.txt", file_get_contents("php://input"));
http_response_code(200);
// parse event (which is json string) as object
// Do something - that will not take long - with $event
$evt = json_decode($input, true);

    
    switch ($evt['Status']) {//begin event switch

        case 0:
            //waiting

        break;

        case 1:
            //in progress

        break;

        case 2:
            //sent
            $msgid = $evt['MsgId'];
            $sentdate = $evt['SendDateTime'];
            $remoteid = $evt['remoteid'];

            $status = "Sent";

            $uph = $mydbcon->prepare("UPDATE sms_send SET sent_date=?, status=? WHERE ticket=? AND remoteid=?");
            $uph->bind_param('ssss', $sentdate, $status, $msgid, $remoteid);
            $uph->execute(); 

        break;

        case 3:
            //delivered
            $msgid = $evt['MsgId'];
            $sentdate = $evt['SendDateTime'];
            $deliverydate = $evt['DeliveryDateTime'];
            $remoteid = $evt['remoteid'];

            $status = "Delivered";
            
            $uph = $mydbcon->prepare("UPDATE sms_send SET sent_date=?, delivery_date=?, status=? WHERE ticket=? AND remoteid=?");
            $uph->bind_param('sssss', $sentdate, $deliverydate, $status, $msgid, $remoteid);
            $uph->execute();

        break;

        case 4:
            //refused

        break;

        case 6:
            //not delivered

        break;



    }//end event switch





//end of webhook
exit();
}
?>

Someone help me with how to access and save the values to my db.

Thanks for your help in advance.


Solution

  • Your response does not contain JSON and thus json_decode() is likely returning false and throwing an exception. You should turn error reporting on to capture and debug your code.

    Since the response is URL encoded, PHP natively supports parse_str() to do this. This will convert the URL to an associative array meaning no further changes to your code is needed.

    $response = 'MsgId=fd1c39f0-e443-11ec-8460-00.........';
    $event    = [];
    
    // ['MsgId' => 'fd1c39f0-e443-11ec-8460-00........']
    parse_str(urldecode($response), $event);
    

    See it working over at 3v4l.org