In just a few lines of code, your PHP application can send, receive, and reply to text messages with Twilio Programmable SMS.
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:
- Sign up for Twilio and purchase an SMS-enabled phone number
- Check and install any prerequisites manually
- Send your first SMS
- Set up your development environment to send and receive messages
- Receive inbound text messages
- Reply to incoming messages with a return SMS
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.
- When you sign up, you'll be asked to verify your personal phone number. This helps Twilio verify your identity and also allows you to send test messages to your phone from your Twilio account while in trial mode.
- Once you verify your number, you'll be asked a series of questions to customize your experience.
- Once you finish the onboarding flow, you'll arrive at your project dashboard in the Twilio Console. This is where you'll be able to access your Account SID, authentication token, find a Twilio phone number, and more.
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.
Once you have installed Homebrew, run the following command to install
brew tap twilio/brew && brew install twilio
The suggested way to install
twilio-cli is by using Scoop, a command-line installer for Windows. If you don’t already have it installed, visit the Scoop site for installation instructions and then return here.
Note PowerShell will need to be run as an administrator to avoid common permission issues when installing via Scoop.
- Add the
scoop bucket add twilio-scoop https://github.com/twilio/scoop-twilio-cli
- Install the app:
scoop install twilio
twilio-cli can be installed using the Advanced Package Tool (
apt) on most distributions such as Debian, Ubuntu, and Mint.
To do so, run the following commands in your terminal:
wget -qO- https://twilio-cli-prod.s3.amazonaws.com/twilio_pub.asc \ | sudo apt-key add - sudo touch /etc/apt/sources.list.d/twilio.list echo 'deb https://twilio-cli-prod.s3.amazonaws.com/apt/ /' \ | sudo tee /etc/apt/sources.list.d/twilio.list sudo apt update sudo apt install -y twilio
For other installation methods, see the Twilio CLI Quickstart.
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.
If you’re using a Mac or *nix machine, you may have PHP already installed. In your favorite terminal, run:
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.
- Create a new project directory, or navigate to your project's working directory.
- In that directory, download the most recent version of the Twilio PHP Helper Library from Github.
- Unzip the PHP helper library in the directory.
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:
In the code, switch the placeholders in
auth_token with your Twilio credentials. Visit Twilio Console to find your unique Account SID and Auth Token to substitute.
Replace the values for
auth_token with your unique values.
Note: While it's easy 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.
[+][country code][phone number including area code]
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:
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.
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 webserver 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)
Save the file, then start the PHP development server with:
php -S localhost:8000
In your favorite browser, open the URL http://localhost:8000/reply_sms.php.
If all went smoothly, you should see XML in your browser with our message. And, yes, that's all the code needed - there are just a few more steps before everything is ready to go. Next, let's expose this endpoint to Twilio.
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
Then, you need to configure your Twilio phone number to call your webhook URL whenever a new message comes in:
- Go to Phone Numbers > Active Numbers in the Twilio Console.
- Select the SMS-enabled Twilio number you want to use.
- For the A MESSAGE COMES IN webhook, enter the ngrok URL you copied down earlier. Append
/reply_sms.phpto 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 -
- Your PHP dev server will note a new connection
- Twilio will forward your response back as an SMS!
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.
- Our extensive API Reference documentation for Twilio SMS
- Learn how to create an SMS conversation in PHP
- Track the delivery status of your messages with PHP
- Try TwilioQuest, our interactive, self-paced game that teaches you how to Twilio.
We hope you enjoyed the quickstart, and definitely can't wait to see what you build!