This tutorial builds a PHP webhook endpoint that Twilio calls every time your number receives an inbound SMS, reads the sender and message body from the POST parameters, and responds with valid TwiML XML. It targets PHP developers building two-way SMS applications where your server must process and reply to messages users send to your Twilio number. You will expose a local PHP script via ngrok, configure your Twilio number's webhook, and test the full round-trip by sending a message to your number.
Prerequisites and Setup
Install the Twilio SDK with composer require twilio/sdk and download ngrok to create a public HTTPS tunnel to your local PHP development server. Run php -S localhost:8000 to start a built-in PHP server, then run ngrok http 8000 in a separate terminal to get the public HTTPS URL. In the Twilio Console, open your phone number's configuration and set the Messaging webhook URL to your ngrok HTTPS address followed by the path to your PHP file, such as /webhook.php. Set the HTTP method to POST, which is the default for SMS webhooks.
composer require twilio/sdk
# Terminal 1: start PHP built-in server
php -S localhost:8000
# Terminal 2: start ngrok
ngrok http 8000
# Paste https://xxxx.ngrok.io/webhook.php into Twilio Console SMS webhook
The Complete Code
Twilio sends inbound SMS data as POST parameters so you read them with $_POST['Body'] for the message text and $_POST['From'] for the sender's number. The MessagingResponse class from the Twilio PHP SDK builds a valid TwiML XML response without you needing to construct XML strings by hand. Setting the Content-Type response header to text/xml before echoing the TwiML ensures Twilio parses the response correctly and does not log a content-type warning. The script must return a 200 HTTP status, which PHP does by default, or Twilio marks the webhook delivery as failed and retries.
<?php
require_once 'vendor/autoload.php';
use TwilioTwiMLMessagingResponse;
$from = $_POST['From'] ?? '';
$body = trim($_POST['Body'] ?? '');
error_log('Inbound SMS from ' . $from . ': ' . $body);
$response = new MessagingResponse();
$response->message('Got your message: "' . $body . '". We will reply shortly.');
header('Content-Type: text/xml');
echo $response;
Testing and Debugging
Send a text from your personal phone to your Twilio number and check the PHP error log for the error_log output confirming the inbound POST was received. If Twilio reports a webhook error in the Console, check that your ngrok tunnel is still running and that the webhook URL path in the Console matches the exact filename of your PHP script. Return a Content-Type: text/xml header or Twilio may log a warning about an unexpected response format even if the TwiML XML content is valid. Use Twilio's webhook inspector in the Console to replay recent inbound messages against your webhook URL during debugging without needing a real phone.
Taking It Further
Validate the incoming Twilio signature by using the TwilioSecurityRequestValidator class to verify each POST genuinely came from Twilio and reject requests that fail signature verification. Store each inbound message in a database with the sender, body, timestamp, and any reply sent for conversation history and analytics. Add keyword detection logic inside the handler to branch responses based on the message content, which is the foundation of a PHP SMS bot. Deploy the webhook to a production PHP host with a stable HTTPS URL and update the Twilio Console to remove the ngrok dependency.
Talk to Telphi Consulting to build a production inbound SMS handler with request validation, logging, and automated response logic in PHP.
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)
Be the first to comment
No comments yet. Share your thoughts below.