Back to Blog
Language How-Tos

How to Track Message Delivery Status in Twilio Using PHP

Build a PHP StatusCallback endpoint that receives Twilio delivery webhooks and logs message status updates to your database.

DA
Danial A
Senior Twilio Consultant, Telphi Consulting
June 22, 2026
8 min read
Twilio
PHP
How-To
Code
How to Track Message Delivery Status in Twilio Using PHP

This tutorial builds a PHP webhook endpoint that receives real-time delivery status updates from Twilio for every SMS your application sends, enabling you to track whether each message was delivered, failed, or bounced. It targets PHP developers who need reliable delivery confirmation for notifications, compliance logging, or retry logic. You will add a statusCallback URL when creating messages and process the incoming status POSTs in a dedicated PHP script.

Prerequisites and Setup

Install the packages with composer require twilio/sdk vlucas/phpdotenv and expose your local PHP server with ngrok to get a public HTTPS URL for the callback endpoint. Twilio POSTs status updates at multiple points in the message lifecycle: queued, sent, delivered, and failed or undelivered, so your script will receive several POSTs per message. The MessageSid field in each callback lets you correlate status updates with the original send in your database. Your endpoint must respond with a 200 HTTP status or Twilio will mark the callback as failed and retry it, which can produce duplicate status entries if your handler is not idempotent.

composer require twilio/sdk vlucas/phpdotenv

# Start ngrok
ngrok http 8000
# Use the https URL as your statusCallback

The Complete Code

Add a 'statusCallback' key to the options array in messages->create() pointing to the public URL of your status.php script. The status.php endpoint reads $_POST['MessageSid'], $_POST['MessageStatus'], $_POST['To'], and $_POST['ErrorCode'] to extract the delivery event details. Writing each update to an error log and flagging failed or undelivered statuses for immediate attention gives you operational visibility without a full database setup. The script sends back a 200 HTTP response by default since PHP does not set an error status unless you explicitly call http_response_code() with a different value.

<?php
// send-with-tracking.php
require_once 'vendor/autoload.php';

use TwilioRestClient;

$dotenv = DotenvDotenv::createImmutable(__DIR__);
$dotenv->load();

$client = new Client($_ENV['TWILIO_ACCOUNT_SID'], $_ENV['TWILIO_AUTH_TOKEN']);

$message = $client->messages->create(
    '+15551234567',
    [
        'from'           => $_ENV['TWILIO_PHONE_NUMBER'],
        'body'           => 'Test delivery tracking from PHP.',
        'statusCallback' => 'https://xxxx.ngrok.io/status.php',
    ]
);
echo 'Sent: ' . $message->sid . PHP_EOL;
<?php
// status.php
$sid    = $_POST['MessageSid']    ?? '';
$status = $_POST['MessageStatus'] ?? '';
$to     = $_POST['To']            ?? '';
$error  = $_POST['ErrorCode']     ?? '';

$logLine = date('Y-m-d H:i:s') . ' [STATUS] ' . $sid . ' -> ' . $status;
if ($error) { $logLine .= ' (error ' . $error . ')'; }
error_log($logLine);

if ($status === 'failed' || $status === 'undelivered') {
    error_log('ALERT: Delivery failure to ' . $to . ' code ' . $error);
}

http_response_code(204);

Testing and Debugging

Run send-with-tracking.php, then tail the PHP error log and watch for a sequence of status POSTs: typically queued, then sent, then delivered within a few seconds for a successful SMS. If no callbacks arrive, verify the ngrok tunnel is running and the statusCallback URL in your send script points to the correct ngrok HTTPS address followed by /status.php. Check the Twilio Console under Monitor for webhook delivery attempts and any HTTP error codes returned by your endpoint. Error code 30003 in a status callback indicates the destination handset was unreachable; error code 30008 means the carrier blocked the message.

Taking It Further

Replace the error_log calls with PDO inserts to a delivery tracking table that stores the SID, status, timestamp, and error code for a full audit trail. Write a retry function that reads failed and undelivered entries from the database and re-creates those messages after a configurable delay using a Laravel scheduled command or a plain cron entry. Build a simple reporting page in Laravel that aggregates delivery rates by date, destination country, and error code so you can identify carrier issues quickly. Alert your team via a Slack webhook when the failure rate for a sending session exceeds a configurable threshold by checking the ratio in the status callback handler.

Contact Telphi Consulting to implement a delivery monitoring and alerting system for your Twilio SMS operations in PHP.

Share this article:
0 views

Ready to Transform Your Business Communications?

Get a free consultation with our VoIP experts and discover how we can help you save costs, improve efficiency, and scale your business.

Comments (0)

Join the discussion and share your thoughts (AI-moderated for quality)

Protected by AI moderation

Be the first to comment

No comments yet. Share your thoughts below.