You can use this example as a basis for your own PHP Webhook:
<?php
$entityBody = file_get_contents('php://input');
$messages = json_decode(utf8_encode($entityBody));
if (json_last_error()) {
$warning = "JSON parsing error: " . json_last_error();
print $warning;
error_log($warning);
return;
}
foreach ($messages as &$message) {
if ($message->{'type'} === 'RECEIVED') {
printf ("Message received from [%s] ID [%s] body [%s]", $message->{'from'}, $message->{'id'},
$message->{'body'});
} else if ($message->{'type'} === 'SENT') {
printf ("Message sent to [%s] ID [%s] body [%s]", $message->{'to'}, $message->{'id'},
$message->{'body'});
}
}
?>
To test it locally on your own computer, save the above to a file called callback.php
, and then run a local PHP server in the same folder where the file was saved to:
php -S localhost:8188
Now execute a sample JSON callback against the Web server, from another terminal:
curl -X POST -H 'Content-Type: application/json' -d '[ { "id": "30000000001", "type": "RECEIVED", "from": "44987654321", "to": "44123456789", "body": "This is a test", "encoding": "TEXT", "protocolId": 0, "messageClass": 2, "numberOfParts": 1, "creditCost": 0, "submission": { "id": "2-30000000001", "date": "2019-08-05T11:30:12Z"}, "status": { "id": "DELIVERED.null", "type": "DELIVERED" }, "relatedSentMessageId": null, "userSuppliedId": null } ]' http://localhost:8188/callback.php
Message received from [44987654321] ID [30000000001] body [This is a test]