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](https://ngrok.com/)
* [Deploying PHP to Heroku](http://net.tutsplus.com/tutorials/php/quick-tip-deploy-php-to-heroku-in-seconds/)
* [Deploying a PHP App on Google App Engine](https://developers.google.com/appengine/docs/php/gettingstarted/introduction)
* [Deploying a PHP app on Dotcloud](http://docs.dotcloud.com/services/php/)

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 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. https://monkey.ngrok.io/sms-hello-monkey.php) 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.
    <?php
        header("content-type: text/xml");
        echo "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n";
    ?>
    <Response>
        <Message>Hello, Mobile Monkey</Message>
    </Response>
    
    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, 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> (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

<Response>
    <Message>
        <Body>Hello, Mobile Monkey</Body>
        <Media>https://demo.twilio.com/owl.png</Media>
    </Message>
</Response>

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.