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.
To send an outgoing SMS message perform an HTTP POST to the SMS Messages resource URI. We will also use the Twilio PHP Library for making REST requests.
<?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->sms_messages->create(
// 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 <Sms> verb.
For more information about sending SMS messages, see the SMS REST resource documentation.