Tuesday, 26 March 2013

Twilio API: Sending a Text Message


Twilio API: Sending a Text Message

I’ve been experimenting with the Twilio API recently and have found it very easy to automatically send text messages or call a telephone programatically. In fact, you are able to send a text message in just a few lines of code. Such a service can use used to validate mobile phone numbers for an app, much like how Facebook does.
Using the Twilio PHP library, sending a message can be achieved in a few lines of code.
1234567891011121314151617
<?php
 
require 'Services/Twilio.php';
 
$sid = "aaabbbcccdddeeefffggg"; // Your Account SID from www.twilio.com/user/account
$token = "hhhiiijjjkkklllmmmnnnooo"; // Your Auth Token from www.twilio.com/user/account
 
$client = new Services_Twilio( $sid, $token );
$message = $client->account->sms_messages->create(
'+44xxx', // From a valid Twilio number
'+44yyy', // Text this number
"The message to send"
);
 
echo $message->sid;
 
?>
view rawtwilio.phpThis Gist brought to you by GitHub.
The above 12 lines of code is enough to send a simple message to any mobile phone. Making a phone is even easier, and I’ll cover that in a later post.
Twilio doesn’t have a JavaScript SDK, unfortunately, so you would have to create a server-side API (using PHP) and then use JavaScript (with jsonp) to call the API. This again is quite straight forward to do in JavaScript, but requires more coding on the back-end (PHP) to secure the script.

No comments:

Post a Comment