Skip to contentSkip to navigationSkip to topbar
On this page

Programmable Messaging PHP Quickstart


(information)

Info

Ahoy there! All messaging transmitted using Twilio's messaging channels is treated as Application-to-Person (A2P) messaging and subject to Twilio's Messaging Policy(link takes you to an external page). For detailed information on policy rules to ensure you remain compliant while using Twilio's services, please see our Acceptable Use Policy(link takes you to an external page).

In 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 CLI, and the Twilio PHP helper library.

We'll use the package manager Composer to manage our product dependencies. If you don't want to use Composer, no problem — we also have a separate non-Composer PHP SMS Quickstart.

In this Quickstart, you'll learn how to:

  1. Sign up for Twilio and purchase an SMS-enabled phone number.
  2. Check and install any prerequisites using Composer(link takes you to an external page).
  3. Send your first SMS.
  4. Set up your development environment to send and receive messages.
  5. Receive inbound text messages.
  6. Reply to incoming messages with a return SMS.

Sign up — or into — Twilio

sign-up--or-into--twilio page anchor
(information)

Info

Already have a Twilio account? Go ahead and skip this section.

(warning)

Warning

If you are sending SMS to the U.S. or Canada, before proceeding further please be aware of updated restrictions on the use of Toll-Free numbers for messaging, including TF numbers obtained through Free Trial. Please click here(link takes you to an external page) for details.

You can sign up for a free Twilio trial account here(link takes you to an external page).

  • 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(link takes you to an external page). 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(link takes you to an external page) page, check the SMS box and click Search.

Buy a twilio phone number.

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.

Select an SMS-enabled phone number.

We'll need to use the Twilio CLI (command line interface) for a few tasks, so let's install that now.

macOSWindowsLinux

The suggested way to install twilio-cli on macOS is to use Homebrew(link takes you to an external page). If you don't already have it installed, visit the Homebrew site(link takes you to an external page) 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
(information)

Info

For other installation methods, see the Twilio CLI Quickstart.

Log in to the Twilio CLI

log-in-to-the-twilio-cli page anchor

Run twilio login to get the Twilio CLI connected to your account. Visit the Twilio Console(link takes you to an external page), 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.


Install PHP and the Twilio PHP Helper Library

install-php-and-the-twilio-php-helper-library page anchor
(information)

Info

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. When doing web development in PHP, we strongly suggest using Composer(link takes you to an external page) for package management. This Quickstart relies on Composer to install the PHP Helper library. If you choose not to use Composer, please visit our non-Composer PHP SMS Quickstart.

Install PHP

install-php page anchor

If you're using a Mac or NIX machine, you may have PHP already installed. In your favorite terminal, run:

php --version

If it is not installed, follow the PHP installation instructions(link takes you to an external page).

If you're using a Windows machine, please follow the official PHP tutorial to install PHP(link takes you to an external page).

(error)

Danger

While many versions of PHP 5.x and PHP 7.x will work for this Quickstart, please pay attention to supported PHP releases(link takes you to an external page). Always update un-supported versions when doing web development, as older versions will not receive security updates.

Composer is the de facto standard package manager for PHP web development. If you haven't yet installed it, here are the installation instructions for your platform:

If you'd prefer to install dependencies manually, check out our non-Composer PHP SMS Quickstart.

Install the Twilio PHP Helper Library

install-the-twilio-php-helper-library page anchor

We need to install the Twilio PHP Helper Library in the directory where you will complete the Quickstart. If you are using Composer, there are two ways to do this.

First, from a terminal, you can run the following command:

composer require twilio/sdk

Alternatively, you can create a file named composer.json and then add the following to it:

1
{
2
"require": {
3
"twilio/sdk": "^5.0"
4
}
5
}
6

Then run

composer install

And composer will grab the latest version of the Twilio PHP Helper Library.

Finally, for a non-composer installation follow the instructions here(link takes you to an external page). You'll also need to change the code samples as they appear in this Quickstart as shown in those instructions(link takes you to an external page).


Send an outbound SMS with PHP

send-an-outbound-sms-with-php page anchor

Now that we have PHP, Composer, and twilio-php installed, we can make a single API request and send an outbound text message from our Twilio phone number. Create and open a new file called send_sms.php and type or paste in this code sample.

Edit your Account Sid, Auth Token, and change the Twilio Number to a SMS-capable Twilio phone number to send an SMS with PHP.

1
<?php
2
require __DIR__ . '/vendor/autoload.php';
3
use Twilio\Rest\Client;
4
5
// Your Account SID and Auth Token from twilio.com/console
6
// To set up environmental variables, see http://twil.io/secure
7
$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_AUTH_TOKEN"]
11
12
// A Twilio number you own with SMS capabilities
13
$twilio_number = "+15017122661";
14
15
$client = new Client($account_sid, $auth_token);
16
$client->messages->create(
17
// Where to send a text message (your cell phone?)
18
'+15558675310',
19
array(
20
'from' => $twilio_number,
21
'body' => 'I sent this message in under 10 minutes!'
22
)
23
);

Replace the placeholder Twilio credentials

replace-the-placeholder-twilio-credentials page anchor

Swap the placeholders in account_sid and auth_token with your personal Twilio credentials. Replace the values for account_sid and auth_token with your unique values.

(error)

Danger

While it's easier to hardcode your credentials in a file for this quickstart, you should use environment variables to keep them secret in production. Check out how to set environment variables(link takes you to an external page) for more information, and see the code comments for an example of how to read them in PHP. This repo(link takes you to an external page) is also an excellent resource for dealing with environment variables.

Replace the twilio_number phone number

replace-the-twilio_number-phone-number page anchor

Earlier, you purchased an SMS-enabled phone number(link takes you to an external page). Paste that number into the twilio_number variable using E.164 formatting:

[+][country code][phone number including area code]

Replace the 'To' number in the create() call

replace-the-to-number-in-the-create-call page anchor

The first value in the call to create() is the outgoing phone number, currently set to +15558675310. This can be any phone number that can receive text messages, but you should use a number you control first to witness the magic! As above, use E.164 formatting for this number.

Save the file then run the script:

php send_sms.php

Boom! Assuming all the values are correct, you should already see the SMS from your Twilio number on your phone.

Are your customers in the US or Canada? You can also send them MMS messages by adding just one line of code. Check out this guide to sending MMS to see how to do it.

(warning)

Warning

If you are on a Twilio Trial account, your outgoing SMS messages are limited to phone numbers that you have verified with Twilio. Phone numbers can be verified via your Twilio Console's Verified Caller IDs(link takes you to an external page).


Receive and reply to inbound SMS messages with PHP

receive-and-reply-to-inbound-sms-messages-with-php page anchor

When someone sends an SMS to your Twilio phone number, Twilio makes an HTTP POST 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 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.

(information)

Info

If you're not using the same directory as before, please follow the PHP Helper Library install step above.

Receive and Reply to an SMS in PHP

receive-and-reply-to-an-sms-in-php page anchor

Demonstrates using the Twilio PHP Helper Library to receive an SMS and send one in reply from your application.

1
<?php
2
require_once "vendor/autoload.php";
3
use Twilio\TwiML\MessagingResponse;
4
5
// Set the content-type to XML to send back TwiML from the PHP Helper Library
6
header("content-type: text/xml");
7
8
$response = new MessagingResponse();
9
$response->message(
10
"I'm using the Twilio PHP library to respond to this SMS!"
11
);
12
13
echo $response;

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(link takes you to an external page).

If all went well, you should see TwiML in your browser with the message we'd like to reply with to all our inbound texts. And, yes, that's all the code you need. But there's just one more step you need to perform before everything is wired up.

(information)

Info

If you don't already have ngrok downloaded and configured, follow these instructions(link takes you to an external page) to do so.

Configure ngrok and your webhook URL

configure-ngrok-and-your-webhook-url page anchor

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(link takes you to an external page).

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:

  1. Go to Phone Numbers > Active Numbers in the Twilio Console(link takes you to an external page).
  2. Select the SMS-enabled Twilio number you want to use.
  3. For the A MESSAGE COMES IN webhook, enter the ngrok URL you copied down earlier. Append /reply_sms.php to the end of the URL.

Test your application with a text

test-your-application-with-a-text page anchor

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:

  1. Your PHP dev server will note a new connection.
  2. 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:

We hope you enjoyed the quickstart, and definitely can't wait to see what you build!

Need some help?

Terms of service

Copyright © 2024 Twilio Inc.