Receive and Reply to SMS and MMS Messages in PHP
In this guide, we'll walk through how to use Programmable SMS to respond to incoming SMS messages in a PHP web application.
When someone sends a text message to a Twilio number, Twilio can call a webhook you create in PHP from which you can send a reply back using TwiML. This guide will help you master those basics in no time.
Twilio can send your web application an HTTP request when certain events happen, such as an incoming text message to one of your Twilio phone numbers. These requests are called webhooks, or status callbacks. For more, check out our guide to Getting Started with Twilio Webhooks. Find other webhook pages, such as a security guide and an FAQ in the Webhooks section of the docs.
The code snippets in this guide are written using PHP version 7.1, assume a local webserver exists, and make use of the Twilio PHP SDK.
Let's get started!
Creating your webhook
Later we will configure a Twilio phone number to send an HTTP request to a URL that we specify every time someone sends that number a message. Before we set that up, let's create the PHP code that will handle receiving and replying to those messages.
We'll create a new PHP file
replyToMessage.php to hold our code. If you haven't already installed the Twilio helper library, you'll want to do that now.
Here we're using the helper library to generate and send TwiML back to the Twilio API which will reply to incoming messages using those instructions.
Now we need a public URL where the Twilio API can send incoming requests.
For a real project we'll want to deploy our code on a web or cloud hosting provider (of which there are many), but for learning or testing purposes, it's reasonable to use a tool such as ngrok to create a temporary public URL for our local web server.
Configure your Twilio number
Now that we have a URL for our code, we want to add that to the configuration of a Twilio number we own:
- Log into Twilio.com and go to the Console's Numbers page
- Click on the phone number we want to use for this project
- Find the Messaging section and the "A MESSAGE COMES IN" option
- Select "Webhook" and paste in the URL we want to use, being sure to include the relevant file name:
If we wanted our webhook to receive another type of request, like
PUT, we could specify that using the drop down to the right of our URL.
Backup webhook URL
You'll notice in the console that there is also a spot to provide a Webhook URL for when the "PRIMARY HANDLER FAILS." Twilio will call this URL if your primary handler returns an error or does not return a response within 15 seconds. Refer to our Availability and Reliability guide for more details on the fallback URL.
Protect your webhooks
Twilio supports HTTP Basic and Digest Authentication. Authentication allows you to password protect your TwiML URLs on your web server so that only you and Twilio can access them.
Learn more about HTTP authentication and validating incoming requests here.
Respond with media (MMS message)
We can send an MMS by using the
media method to add an image URL to our message. We can send multiple images by adding more
media calls with additional image URLs.
After updating the code, you may need to restart your local server. When you text the Twilio number you just set up with your webhook, you should get a reply back with the added image! Check out the API Reference for more info.
MMS messages can only be sent and received by numbers which have MMS capability. You can check the capabilities of numbers in the account portal or query the Available Phone Numbers resource to search for Twilio numbers that are MMS enabled.
Custom responses to incoming SMS messages
We've sent text and image replies, but what if we want to choose our reply based on the message we received? No problem!
The content of the incoming message will be in the
Body parameter of the incoming API request. Let's take a look at how we might use that to customize our response.
Here we've created a default message and an array of options for replying to the specific incoming message "never gonna." If any messages come in other than "never gonna" then we send out our default message. When the incoming message is "never gonna" we'll pick one of our array of options at random. Once we've updated our code, we can try sending our Twilio number an SMS that says "never gonna" or anything else and should get the corresponding response.
Enhance messages with add-ons
Need more information about the phone number that sent the message? Need to analyze the SMS itself for sentiment or other data? Add-ons are available in the Add-ons Marketplace to accomplish these tasks and more.
To learn how to enable Add-ons for your incoming SMS messages, refer to our Add-ons quickstart.
Where to next?
Ready to dig deeper into handling incoming messages? Check out our guide on how to Create an SMS Conversation and our Automated Survey tutorial.
Need some help?
We all do sometimes; code is hard. Get help now from our support team, or lean on the wisdom of the crowd by visiting Twilio's Stack Overflow Collective or browsing the Twilio tag on Stack Overflow.