In just a few lines of code, your PHP application can send, receive, and reply to text messages with Twilio Programmable SMS.
This PHP SMS Quickstart shows you how to use our Communications REST API, the Twilio PHP helper library, and the Twilio CLI. We'll install all dependencies manually without using a package manager.
We highly recommend trying Composer for package and dependency management in your PHP web applications. If interested, try our standard PHP SMS Quickstart.
In this quickstart, you'll learn how to:
Already have a Twilio account and SMS-capable number? Go ahead and skip this section.
You can sign up for a free Twilio trial account here.
If you don't currently own a Twilio phone number with SMS functionality, you'll need to purchase one. After navigating to the Buy a Number page, check the SMS box and click Search.
You'll then see a list of available phone numbers and their capabilities. Find a number that suits your fancy and click Buy to add it to your account.
We'll need to use the Twilio CLI (command line interface) for a few tasks, so let's install that next.
The suggested way to install twilio-cli
on macOS is to use Homebrew. If you don't already have it installed, visit the Homebrew site for installation instructions and then return here.
Once you have installed Homebrew, run the following command to install twilio-cli
:
brew tap twilio/brew && brew install twilio
For other installation methods, see the Twilio CLI Quickstart.
Run twilio login
to get the Twilio CLI connected to your account. Visit the Twilio Console, and under Account Info, you'll find your unique Account SID and Auth Token to provide to the CLI.
Next, we need to install PHP and the Twilio PHP Helper Library.
If you already have PHP and the Twilio PHP Helper Library installed in your working directory, feel free to skip this step and move on to sending your first text message.
To send your first SMS, let's make sure you're set up with PHP and able to install Twilio's PHP Helper library. This quickstart will show you how to install packages manually.
When doing web development in PHP, we strongly suggest using Composer for package management. You can try the standard PHP SMS Quickstart if interested.
If you're using a Mac or NIX machine, you may have PHP already installed. In your favorite terminal, run:
php --version
If not installed, follow the PHP installation instructions.
If you're using a Windows machine, follow the official PHP tutorial to install PHP.
Many versions of PHP 5.x and PHP 7.x will work for this quickstart, but please pay careful attention to supported PHP releases. Always update your un-supported versions when doing web development as older versions will not receive security updates.
You'll need to install the Twilio PHP Helper Library in the directory where you will complete the quickstart.
Now that we have PHP and twilio-php-main
installed manually, we can make a single API request and send an outbound text message from a Twilio phone number.
Create and open a new file called send_sms.php
and type or paste in this code sample:
Send an outbound SMS using PHP and the Twilio PHP Helper Library installed without Composer.
1<?php2// Include the bundled autoload from the Twilio PHP Helper Library3require __DIR__ . '/twilio-php-main/src/Twilio/autoload.php';4use Twilio\Rest\Client;5// Your Account SID and Auth Token from twilio.com/console6// To set up environmental variables, see http://twil.io/secure7$account_sid = getenv('TWILIO_ACCOUNT_SID');8$auth_token = getenv('TWILIO_AUTH_TOKEN');9// In production, these should be environment variables. E.g.:10// $auth_token = $_ENV["TWILIO_ACCOUNT_SID"]11// A Twilio number you own with SMS capabilities12$twilio_number = "+15017122661";13$client = new Client($account_sid, $auth_token);14$client->messages->create(15// Where to send a text message (your cell phone?)16'+15558675310',17array(18'from' => $twilio_number,19'body' => 'I sent this message in under 10 minutes!'20)21);
In the code, switch the placeholders in account_sid
and auth_token
with your Twilio credentials. Visit Twilio Console to find your unique Account SID and Auth Token to substitute.
Replace the values for account_sid
and auth_token
with your unique values.
While it's faster to hardcode your credentials in a file for a quickstart, use environment variables to keep them secret in production. Check out how to set environment variables for more information, and see the code comments for an example of how to read them in PHP. This repo is also an excellent resource for dealing with environment variables.
Earlier, you purchased an SMS-enabled phone number. Paste that number into the twilio_number
variable using E.164 formatting:
[+][country code][phone number including area code]
For example, +18005551212
.
The first value in the API call to create() is the outgoing phone number, currently set to +15558675310
. This can be any phone number that can receive texts... but use a number you control to witness the magic! As above, use E.164 formatting for this number.
Save the file then run the script:
php send_sms.php
Assuming all the values are correct, you should already see the SMS from your Twilio number on your phone!
If you are on a Twilio Trial account, your SMS messages are limited to phone numbers that you have verified with Twilio. You can verify phone numbers via the Twilio Console's Verified Caller IDs.
Are your customers in the U.S. or Canada? You can also send MMS messages by adding just a single line of code. Check out this guide to sending MMS to see how.
When someone sends an SMS to your Twilio phone number, Twilio makes an HTTP request to your server asking for instructions on what to do next. For this quickstart, we'll reply to the sender with a note about how we're sending our SMS reply.
We'll again use the Twilio PHP Library, then use PHP's built-in development web server in combination with a tool called ngrok to instruct Twilio how to handle the message. Create a new file reply_sms.php
in the same directory as send_sms.php
, open it, then copy and paste or type the following code.
(Note: if you do not use the same directory, please follow the PHP Helper Library install step above)
We'll use ngrok to set up a tunnel from the public internet to your localhost. This will let us use a public URL as the webhook for your application.
First, download and configure ngrok.
Next, run this command to have ngrok set up a tunnel to your localhost:
ngrok http 8000
This will start an ngrok tunnel. Copy down the Forwarding URL that ends with ngrok.io
.
Then, you need to configure your Twilio phone number to call your webhook URL whenever a new message comes in:
/reply_sms.php
to the end of the URL.Now that everything is glued together, it's time to test.
Send a text message from your mobile phone to your Twilio phone number. You'll see a couple of things happen very quickly -
Now that you know the basics of sending and receiving SMS and MMS text messages with PHP, you might want to check out these resources.
We hope you enjoyed the quickstart, and definitely can't wait to see what you build!