Twilio SMS Quickstart

Sending Text Messages via the REST API

After responding to so many messages, I think it's time we threw a monkey party. We are going to invite our friends via SMS messages. If you haven't yet, read over the REST Quickstart for voice calls, as this example utilizes concepts introduced in that guide.

To send an outgoing SMS message perform an HTTP POST to the SMS Messages resource URI. We will also use a Twilio Helper Library for making REST requests.

NOTE: If you are using the Twilio Sandbox Number for this example, you will only be able to send SMS messages to phone numbers that you have verified with Twilio. Phone numbers can be verified via your Twilio Account's Phone Numbers Page. The 'From' parameter will also need to be the Twilio Sandbox Number if you are in Free Trial mode.

  • quickstart/sms/sms-rest.php
    <?php
    	/* Send an SMS using Twilio. You can run this file 3 different ways:
    	 *
    	 * - Save it as sendnotifications.php and at the command line, run 
    	 *        php sendnotifications.php
    	 *
    	 * - Upload it to a web host and load mywebhost.com/sendnotifications.php 
    	 *   in a web browser.
    	 * - Download a local server like WAMP, MAMP or XAMPP. Point the web root 
    	 *   directory to the folder containing this file, and load 
    	 *   localhost:8888/sendnotifications.php in a web browser.
    	 */
    	// Include the PHP Twilio library. You need to download the library from 
    	// twilio.com/docs/libraries, and move it into the folder containing this 
    	// file.
    	require "Services/Twilio.php";
    
    	// set our AccountSid and AuthToken - from www.twilio.com/user/account
    	$AccountSid = "ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX";
    	$AuthToken = "YYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYY";
    
    	// instantiate a new Twilio Rest Client
    	$client = new Services_Twilio($AccountSid, $AuthToken);
    
    	// make an associative array of people we know, indexed by phone number. Feel
    	// free to change/add your own phone number and name here.
    	$people = array(
    		"+14158675309" => "Curious George",
    		"+14158675310" => "Boots",
    		"+14158675311" => "Virgil",
    	);
    
    	// iterate over all our friends. $number is a phone number above, and $name 
    	// is the name next to it
    	foreach ($people as $number => $name) {
    
    		// Send a new outgoing SMS */
    		$sms = $client->account->sms_messages->create(
    			// the number we are sending from, must be a valid Twilio number
    			"YYY-YYY-YYYY", 
    
    			// the number we are sending to - Any phone number
    			$number,
    
    			// the sms body
    			"Hey $name, Monkey Party at 6PM. Bring Bananas!"
    		);
    
    		// Display a confirmation message on the screen
    		echo "Sent message to $name";
        }
    ?>
        

Lets look at the details:

  • First, head over to the Twilio website and log into your Twilio Account page. On the Dashboard there is a section labeled "API Credentials". There you will find your AccountSid and AuthToken. Copy those values and paste them into AccountSid and AuthToken variables.

  • Next, we instantiate a new client object, set the request method to "POST", fill the 'To', 'From' and 'Body' parameters into an associative array, and make the REST API request to Twilio. The 'From' parameter should be the Sandbox phone number for trial accounts or a Twilio phone number you purchased for upgraded accounts.

If your REST request was successful, the SMS has been successfully queued for transmission. The SMS will be sent as soon as possible at a maximum rate of 1 message per second per 'From' phone number.

By default, your application does not get any notification when an SMS is actually sent or when there is a delivery failure. To get a callback when an SMS is sent or when there is a failure, include the "StatusCallback" parameter with your REST request. You can also specify the "statusCallback" parameter on the <Sms> verb.

For more information about sending SMS messages, see the SMS REST resource documentation.


Next: Reply to Incoming SMS