Get Started

Twilio SMS PHP 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, then MMS messages.

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

If you are using a Twilio Trial account 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.
    	 */
    
    	// Step 1: Download the Twilio-PHP library from twilio.com/docs/libraries, 
    	// and move it into the folder containing this file.
    	require "Services/Twilio.php";
    
    	// Step 2: set our AccountSid and AuthToken from www.twilio.com/user/account
    	$AccountSid = "ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX";
    	$AuthToken = "YYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYY";
    
    	// Step 3: instantiate a new Twilio Rest Client
    	$client = new Services_Twilio($AccountSid, $AuthToken);
    
    	// Step 4: make an array of people we know, to send them a message. 
    	// Feel free to change/add your own phone number and name here.
    	$people = array(
    		"+14158675309" => "Curious George",
    		"+14158675310" => "Boots",
    		"+14158675311" => "Virgil",
    	);
    
    	// Step 5: Loop over all our friends. $number is a phone number above, and 
    	// $name is the name next to it
    	foreach ($people as $number => $name) {
    
    		$sms = $client->account->messages->sendMessage(
    
    		// Step 6: Change the 'From' number below to be a valid Twilio number 
    		// that you've purchased, or the (deprecated) Sandbox 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 Account SID and Auth Token. Copy those values and paste them into Account SID and Auth Token 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 <Message> verb.

Sending MMS Messages via the REST API

It's also easy to send an outgoing MMS using Twilio. To send an MMS, you also make an HTTP POST request to the Messages resource but this time specify one or more image media URLs.

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.

Modify the foreach loop as follows:

    // Step 5: Loop over all our friends. $number is a phone number above, and 
    // $name is the name next to it
    foreach ($people as $number => $name) {
 
        $sms = $client->account->messages->sendMessage(
 
        // Step 6: Change the 'From' number below to be a valid Twilio number 
        // that you've purchased, or the (deprecated) Sandbox 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!",
            
            // Step 7: Add a url to the image media you want to send
            array("https://demo.twilio.com/owl.png", "https://demo.twilio.com/logo.png")
        );
 
        // Display a confirmation message on the screen
        echo "Sent message to $name";
    }

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


Next: Reply to Incoming SMS »