SMS messages are a common form of communication for mobile phone users, and a great way to distribute information to several people at once. With Twilio SMS , developers can easily send and receive text messages with local Twilio phone numbers.
Visit the Twilio SMS Quickstart for a step by step introduction to using Twilio SMS.
Twilio exposes outbound SMS messaging functionality through the Twilio REST
API. A POST request to the SMS Messages
resource will send a SMS message to the specified number.
This demo utilizes the [Twilio Helper Library][libs] to interact with the Twilio REST API. Make sure to [Twilio PHP library][http://github.com/twilio/twilio-php/tarball/2.0.2] a copy of the PHP library that contains 'twilio.php' which is referenced in this HowTo.
If you have further questions about Twilio SMS be sure to check out the FAQ.
Say that we run an IT department and want to notify all server admins if a server fails.
Below, we construct an array which contains the names and numbers of our server admins. This array could easily be replaced with a database query to adapt this example for a real-world application.
howtos/sms-notification/sendnotifications.php<?php
// Include the PHP TwilioRest library
require "twilio.php";
// Twilio REST API version
$ApiVersion = "2008-08-01";
// Set our AccountSid and AuthToken
$AccountSid = "ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX";
$AuthToken = "YYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYY";
// Instantiate a new Twilio Rest Client
$client = new TwilioRestClient($AccountSid, $AuthToken);
// make an associative array of server admins
$people = array(
"4158675309"=>"Johnny",
"4158675310"=>"Helen",
"4158675311"=>"Virgil",
);
// Iterate over all our server admins
foreach ($people as $number => $name) {
// Send a new outgoinging SMS by POST'ing to the SMS resource */
// YYY-YYY-YYYY must be a Twilio validated phone number
$response = $client->request("/$ApiVersion/Accounts/$AccountSid/SMS/Messages",
"POST", array(
"To" => $number,
"From" => "YYY-YYY-YYYY",
"Body" => "Bad news $name, the server is down and it needs your help"
));
if($response->IsError)
echo "Error: {$response->ErrorMessage}";
else
echo "Sent message to $name";
}
?>
To send a new SMS message we POST to the Twilio SMS message factory resource. The SMS factory resource only requires three pieces of information. A "To" phone number that will receive the message, a "From" number that will show up as the sender of the message, and a "Body", which is the actual message. The "From" number must be a Twilio-validated phone number.
NOTE: If you are using a trial account with the Twilio Sandbox, you will only be allowed to send SMS messages to phone numbers you have previously validated with Twilio. This restriction does not exist for full accounts.
Twilio queues the SMS for delivery, and responds with
howtos/sms-notification/smsmessage.xml<?xml version="1.0"?>
<TwilioResponse>
<SMSMessage>
<Sid>SM0ab15a14bca78fa582ed7a509f0bde31</Sid>
<AccountSid>AC4bf2dafb92341f7caf8650403e422d23</AccountSid>
<DateCreated>Fri, 08 Jan 2010 11:46:42 -0800</DateCreated>
<DateUpdated>Fri, 08 Jan 2010 11:46:42 -0800</DateUpdated>
<DateSent/>
<To>4158675309</To>
<From>YYYYYYYYYY</From>
<Body>Bad news Johnny, the server is down and it needs your help</Body>
<Status>queued</Status>
<Flags>4</Flags>
<Price/>
</SMSMessage>
</TwilioResponse>
The response from Twilio indicates the message has been queued and will be sent out. To get a notification when the message has been successfully sent or failed, specify the StatusCallback parameter with your request. See the SMS Messages docs docs for more information.
For an overview of all the Twilio SMS API features, take a read through the SMS Quickstart, libs, sms-faq.