PHP Quickstart: Replying to SMS and MMS Messages

Now we're going to walk you through creating a simple PHP Web Application which can receive and reply to SMS and MMS text messages. Mobile Monkey - your application - will quickly sweep the nation... but first we'll have to handle those incoming texts.

Let's walk through creating your first SMS application.

Let Twilio See Your PHP Server

To get Webhooks working, you will need to find a way to expose your server to the public Internet.

You could host with a web provider, or try one of these tutorials that may be able to help you:

If you're looking to get started quickly, try ngrok. You can read more about ngrok on our blog.

If you choose ngrok, your site will be exposed at https://(something).ngrok.io/(document path)

Hello Monkey, SMS Edition

Let's say your web server answers HTTP requests at monkey.ngrok.io.

  1. Create a file called sms-hello-monkey.php in the document root. The URLhttps://monkey.ngrok.io/sms-hello-monkey.php would then be your callback URL.
  2. Browse over to the phone number page in the Twilio console. Click on the phone number you wish to use for the demo. (If you don't have a phone number, purchase an SMS-capable one using the "Buy a Number" button at the top of the page.)
  3. Down the page under 'Webhooks' and 'Messaging', paste the URL of the PHP file (e.g. https://monkey.ngrok.io/sms-hello-monkey.php) in the "A Message Comes In" box.
  4. Save the settings, and this phone number will now be setup to receive SMS messages... once you upload your code.

With the infrastructure plugged in, let's work on the code.

Copy the following code into the sms-hello-monkey.php file on your web server. The PHP Helper Library tells Twilio to reply with an SMS that says "Hello, Mobile Monkey" whenever Twilio receives an SMS for your phone number.

Loading Code Samples...
Language
SDK Version:
  • 4.x
  • 5.x
Format:
  • TwiML
<?php
// Get the Twilio-PHP helper library from twilio.com/docs/libraries/php,
// this line loads the library
require('/path/to/twilio-php/Services/Twilio.php');
use Services_Twilio_Twiml;

$response = new Services_Twilio_Twiml;
$response->message('Hello, Mobile Monkey');

header("content-type: text/xml");
echo $response;
<?php
// Get the Twilio-PHP helper library from twilio.com/docs/libraries/php,
// following the instructions to install it with Composer.
require_once "vendor/autoload.php";
use Twilio\Twiml;

$response = new Twiml();
$response->message('Hello, Mobile Monkey');

header("content-type: text/xml");
echo $response;
<?xml version="1.0" encoding="UTF-8"?>
<Response>
    <Message>
        Hello, Mobile Monkey
    </Message>
</Response>
Reply SMS using Twilio

To test follow these steps: 1. Start your web server (For example, a local test web server might start with a command similar to php -S localhost:8000 1. (If developing locally) Expose your local server. With ngrok, this would be something like ngrok http 8000 1. Send an SMS message to your application's phone number!

Twilio will receive the SMS, make a request to your URL, https://monkey.ngrok.io/sms-hello-monkey.php in our case, and your server will respond with instructions to send a reply SMS message with the body "Hello, Mobile Monkey".

Hello Monkey Phone Message

Respond with an MMS in PHP

  1. Modify your sms-hello-monkey.php on your server specifying the URL of an image as media
  2. Restart your server, then text your Twilio number again.

You should receive a text message in response saying, "Hello, Mobile Monkey", but this time an image will be included!

MMS messages can only be sent and received by numbers having 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.

Loading Code Samples...
Language
SDK Version:
  • 4.x
  • 5.x
Format:
  • TwiML
<?php
// Get the Twilio-PHP helper library from twilio.com/docs/libraries/php,
// this line loads the library
require('/path/to/twilio-php/Services/Twilio.php');
use Services_Twilio_Twiml;

$response = new Services_Twilio_Twiml;
$message = $response->message();
$message->body('Hello, Mobile Monkey');
$message->media('https://demo.twilio.com/owl.png');

header("content-type: text/xml");
echo $response;
<?php
// Get the Twilio-PHP helper library from twilio.com/docs/libraries/php,
// following the instructions to install it with Composer.
require_once "vendor/autoload.php";
use Twilio\Twiml;

$response = new Twiml();
$message = $response->message();
$message->body('Hello, Mobile Monkey');
$message->media('https://demo.twilio.com/owl.png');

header("content-type: text/xml");
echo $response;
<?xml version="1.0" encoding="UTF-8"?>
<Response>
    <Message>
        <Body>Hello, Mobile Monkey</Body>
        <Media>https://demo.twilio.com/owl.png</Media>
    </Message>
</Response>
Reply MMS using Twilio

Note: you can even send multiple images by calling the media('path/img') method to the same instance. Check out the official docs for more details.

Next: Replying to Messages »

And with that, you can receive and reply to SMS and MMS text messages in PHP. Not bad, now sweep the world with your application!

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 browsing the Twilio tag on Stack Overflow.

1 / 1
Loading Code Samples...
SDK Version:
  • 4.x
  • 5.x
Format:
  • TwiML
<?php
// Get the Twilio-PHP helper library from twilio.com/docs/libraries/php,
// this line loads the library
require('/path/to/twilio-php/Services/Twilio.php');
use Services_Twilio_Twiml;

$response = new Services_Twilio_Twiml;
$response->message('Hello, Mobile Monkey');

header("content-type: text/xml");
echo $response;
<?php
// Get the Twilio-PHP helper library from twilio.com/docs/libraries/php,
// following the instructions to install it with Composer.
require_once "vendor/autoload.php";
use Twilio\Twiml;

$response = new Twiml();
$response->message('Hello, Mobile Monkey');

header("content-type: text/xml");
echo $response;
<?xml version="1.0" encoding="UTF-8"?>
<Response>
    <Message>
        Hello, Mobile Monkey
    </Message>
</Response>
SDK Version:
  • 4.x
  • 5.x
Format:
  • TwiML
<?php
// Get the Twilio-PHP helper library from twilio.com/docs/libraries/php,
// this line loads the library
require('/path/to/twilio-php/Services/Twilio.php');
use Services_Twilio_Twiml;

$response = new Services_Twilio_Twiml;
$message = $response->message();
$message->body('Hello, Mobile Monkey');
$message->media('https://demo.twilio.com/owl.png');

header("content-type: text/xml");
echo $response;
<?php
// Get the Twilio-PHP helper library from twilio.com/docs/libraries/php,
// following the instructions to install it with Composer.
require_once "vendor/autoload.php";
use Twilio\Twiml;

$response = new Twiml();
$message = $response->message();
$message->body('Hello, Mobile Monkey');
$message->media('https://demo.twilio.com/owl.png');

header("content-type: text/xml");
echo $response;
<?xml version="1.0" encoding="UTF-8"?>
<Response>
    <Message>
        <Body>Hello, Mobile Monkey</Body>
        <Media>https://demo.twilio.com/owl.png</Media>
    </Message>
</Response>