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:

* [ngrok](
* [Deploying PHP to Heroku](
* [Deploying a PHP App on Google App Engine](
* [Deploying a PHP app on Dotcloud](

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) path)

Hello Monkey, SMS Edition

Let's say your web server answers HTTP requests at

  1. Create a file called sms-hello-monkey.php in the document root. The URL 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 a 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. in the "A Message Comes In" box. 1. 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.

  1. Copy the following code into the sms-hello-monkey.php file on your web server. The XML instructions tell Twilio to reply with an SMS that says "Hello, Mobile Monkey" whenever Twilio receives an SMS for your phone number.
        header("content-type: text/xml");
        echo "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n";
        <Message>Hello, Mobile Monkey</Message>
    1. Start your web server (For example, a local test web server might start with a command similar to php -S localhost:8000
    2. (If developing locally) Expose your local server. With ngrok, this would be something like ngrok http 8000
    3. Send an SMS message to your application's phone number!

Twilio will receive the SMS, make a request to your URL, 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> (see below)
  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.

sms-hello-monkey.php MMS Example

        <Body>Hello, Mobile Monkey</Body>

Note: you can even send multiple images by adding more <Media> elements to your response. 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.