PHP Requires Chilkat v11.0.0+
PHP
Check message delivery status with Twilio
See more Twilio Examples
Fetch an outbound SMS or MMS using the Message SID returned by a live send. A queued message is waiting to be sent. A sent message has been accepted by the upstream carrier. Delivered means Twilio received delivery confirmation. Some routes provide no final delivery receipt.
Read the current status once per lookup. For failed or undelivered messages, display the available delivery error code and text for troubleshooting. A missing message or an HTTP authentication error is a separate result from a delivery failure.
Chilkat PHP Downloads
<?php
use Chilkat2\Http;
use Chilkat2\HttpResponse;
use Chilkat2\JsonObject;
use Chilkat2\StringBuilder;
$success = false;
// Assumes Chilkat is unlocked. See Global Unlock Sample.
// Load an API key for the Twilio account from private application configuration.
$http = new Http();
$http->RequireSslCertVerify = true;
$http->BasicAuth = true;
$http->Login = 'YOUR_API_KEY_SID';
$http->Password = 'YOUR_API_KEY_SECRET';
$url = new StringBuilder();
$url->Append('https://api.twilio.com/2010-04-01/Accounts/');
$url->Append('YOUR_ACCOUNT_SID');
$url->Append('/Messages');
$url->Append('/');
$url->Append('YOUR_MESSAGE_SID');
$url->Append('.json');
$resp = new HttpResponse();
$reply = new JsonObject();
try {
$http->HttpNoBody('GET', $url->GetAsString(), $resp);
$success = true;
} catch (\Chilkat2\Exception $e) {
$success = false;
}
if ($resp->StatusCode == 0) {
echo "No HTTP response from Twilio.\n";
exit;
}
try {
$reply->Load($resp->BodyStr);
$parsed = true;
} catch (\Chilkat2\Exception $e) {
$parsed = false;
}
if ((!$success) || ($resp->StatusCode != 200)) {
echo 'Twilio returned HTTP ', $resp->StatusCode, "\n";
if (($parsed) && ($reply->JsonTypeOf('code') == 2)) {
echo 'Twilio error: ', $reply->IntOf('code'), ' ', $reply->StringOf('message'), "\n";
}
exit;
}
if (!$parsed) {
echo "Expected a JSON response from Twilio.\n";
exit;
}
if ($reply->JsonTypeOf('status') != 1) {
echo "Message status missing from response.\n";
exit;
}
echo 'Delivery status: ', $reply->StringOf('status'), "\n";
if (($reply->StringOfEquals('status', 'failed', true)) || ($reply->StringOfEquals('status', 'undelivered', true))) {
if ($reply->JsonTypeOf('error_code') == 2) {
echo 'Delivery error code: ', $reply->IntOf('error_code'), "\n";
}
if ($reply->JsonTypeOf('error_message') == 1) {
echo $reply->StringOf('error_message'), "\n";
}
}