PHP Quickstart: Replying to SMS and MMS Messages

Let's walk through creating your first SMS application: Mobile Monkey.


  1. You'll need a SMS-enabled Twilio phone number for receiving messages. You can use a free Twilio trial account and get your own dedicated phone number.
  2. You will also need a web host that lets you host web applications. There are lots of web hosting services that will host your application for free or for a few dollars a month.


You can download the (minimal) code used in this example if you want:

Download twilio-php.tar.gz

Hello Monkey, SMS Edition

  1. Let's say your web server answers HTTP requests at Create a file called sms-hello-monkey.php in the document root. The URL will be the SMS URL for your phone number.
  2. Browse over to the phone number configuration page in your account on the Twilio website. Click on the phone number you wish to use for the demo. If you don't have a phone number, you can purchase one using the "Buy a Number" button at the top of the page. Click the "Edit" link next to the phone number you would like to use. Next, check the "SMS" box, and paste the URL of the PHP file (e.g. in the "URL" box. Hit save and this phone number will now be setup to receive SMS messages.
  3. 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>

    Let Twilio find your server

    At this point in the tutorial, you will need to find a way to expose your server to the public Internet.

    Here are some tutorials that may be able to help you:

    For the rest of the tutorial, we will assume your server is available at

  4. To test it, 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

  1. Now modify your sms-hello-monkey.php on your server as follows specifying a URL of an image. If necessary, 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.
        <Body>Hello, Mobile Monkey</Body>

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 »