Home > Software design >  How do I access values from urlencode webhook notification content
How do I access values from urlencode webhook notification content

Time:06-09

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=+2347030202868&OriginatedAddress=&CreateDateTime=2022-06-04 22:22:00&SendDateTime=2022-06-04 22:22:00&DeliveryDateTime=&Reason=002347030202868:20220604222200&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.

CodePudding user response:

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

CodePudding user response:

As Jaquarh pointed out, you're json decoding non-json. You can just use regex or explode as in this little test:

$input = "MsgId=fd1c39f0-e443-11ec-8460-00000a148c01&Status=2&StatusText=sent&DestinationAdress=+2347030202868&OriginatedAddress=&CreateDateTime=2022-06-04 22:22:00&SendDateTime=2022-06-04 22:22:00&DeliveryDateTime=&Reason=002347030202868:20220604222200&SmsCount=1&RSN=000&remoteid=2022082201465615" ;
$input = urldecode($input);
$arr = explode( '&', $input ) ;
$evt = array() ;
foreach( $arr as $pair )
    {
    $keyval = explode( '=', $pair );
    $evt[$keyval[0]] = $keyval[1] ;
    printf( "{$keyval[0]}={$keyval[1]}<br/>") ;
    }
  • Related