Many people want to integrate Short Messaging Service into web applications to send notifications, promotions, reminders to their clients or members from their websites. This can be done using Twilio. Twilio is a cloud communications company that allows developers to programmatically send and receive text messages and make and receive phone calls using its web service APIs. Now we are going to learn how to setup and use Twilio for sending text messages to single/multiple recipients.
In this project I have used The Twilio PHP Helper Library by Twilio. Let’s start with setting up Twilio account first.
Twilio Account Setup:
Step 1: Go for http://twilio.com and Signup for a new Twilio account or Sign in if you already have one.
Step 2: To get started with Twilio you can sign up for a trial account at https://www.twilio.com/try-twilio.
Step 3: Verify your phone number as an outbound caller ID with Twilio. For information on how to verify your phone number, see https://www.twilio.com/user/account/phone-numbers/verified#. For purposes of this example, use the verified phone number as text message recipient in this application.
Note: If you are using a trial account, then its compulsory to use only your own verified phone number for receiving text messages. As an alternative to using an existing number, you can purchase a Twilio phone number.
Step 4: Get account SID and Auth Token from https://www.twilio.com/user/account which will be used in this application.
Step 5: Get your Twilio account phone number from https://www.twilio.com/user/account/phone-numbers/incoming which will be used as sender in text messages.
PHP Code:
We will be working with config.php, example.php, integrate.php and functions.php. Proper understanding of these files is necessary.
config.php:
1 2 3 4 5 6 7 8 9 10 11 12 |
<?php // enter your SID obtained from Twilio account $sid = " your_account_sid "; // enter AUTH Token obtained from Twilio $token = " your_authentication_token "; //Your valid twilio number $my_number = "+12000000000"; ?> |
functions.php:
This file has two functions. send_message function is used to send message to a single recipient. It has two parameters ($receiver and $message). $receiver contains number to which text message will be sent. $message is body of the message.
send_multiple_message function is used to send text messages to multiple recipients. It also has two parameters, the only difference is that $receiver has multiple recipients separated with a comma( , ).
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
<?php // This function will be used to send message to single recipient. function send_message($receiver, $message) { require "config.php"; require './twilio-php/Services/Twilio.php'; $client = new Services_Twilio($sid, $token); $result = $client->account->messages->sendMessage( $my_number, // From a valid Twilio number $receiver, // Text this number $message ); return $result->sid; } // This function will be used to send message to multiple recipient. function send_multiple_message($receiver, $message) { require "config.php"; require './twilio-php/Services/Twilio.php'; $numbers = explode("," , $receiver); $receipt = array(); $count = 0; foreach($numbers AS $num) { $client = new Services_Twilio($sid, $token); $result = $client->account->messages->sendMessage( $my_number, // From a valid Twilio number trim($num), // Text this number $message ); $receipt[$count]["num"] = $num; $receipt[$count]["sid"] = $result->sid; } return $receipt; } ?> |
example.php:
This file can send message to a single recipient. HTML Form with $_POST method is used to send data.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
<?php require_once("functions.php"); if(isset($_POST['receiver'])) { $receiver = $_POST['receiver']; } if(isset($_POST['message'])) { $message = $_POST['message']; } if(isset($receiver) && isset($message) && !empty($message) && !empty($receiver)) { $result = send_message($receiver,$message); if($result) { echo "<h2>Message Sent....</h2><br />SID Returned By Twillio is: $result"; } } ?> <h1>To send Text Message to multiple recipients <a href="multi.php" >Click Here</a></h1><br /> <div class="box"> <script type="text/javascript"> function submit() { document.getElementById("sendsms").submit(); } </script> <form name="sendsms" id="sendsms" action="" method="post"> Send To: <input name="receiver" type="text" /><br /><br /> Message: <textarea style="width: 400px;height:60px;" name="message" ></textarea><br /> <span style="color: #fff;">Message length:160 characters max..</span><br /> <span ><input class="button" type="button" value="Send SMS..." onclick="submit();" /></span> </form> </div> |
integrate.php:
If you want to send message without HTML Form use below code in any desired PHP file.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
<?PHP // include function.php file which contains all functions and SDK files require_once("functions.php"); $receiver = "enter number here"; // example +1589658452 $message = "enter message here"; // max character limit 160 if(isset($receiver) && isset($message) && !empty($message) && !empty($receiver)) { //run the function which will send message to recipient using Twilio. $result = send_message($receiver,$message); if($result) { // output the SID received from Twilio echo "<h2>Message Sent....</h2><br />SID Returned By Twilio is: $result"; } } |
download Twilio text messaging app with PHP - earlysandwich.com.zip
Perfect!
your tutorial, but there is a way to send msn freely? because I guess this is to pay for use the services!.
Thanks for your excellent tutorials, I just see, to the website and is perfect !. 🙂
You can, make a tutorial with drag drop using html5 ajax or javascrit mysqli and
two tables with two container and save automatic in database
for example!. its just, for example!
get data from table one, drag!, and save in the table two, drop!
the table one, with this structure!. (table = students)
id
name_student
first_name
image_the_student
the table two, with this structure!.
id
name_group
image_the_group
now!, get data, from table students,
and show, in container student, inside of tags ul,li for drag the items
and, move the students to the container two for save in the database with ajax automatic for not lost the container of the groups.
thanks, I hope see soon this tutorial. thanks for your time, and take my suggestion. for next tutorial. 🙂
nice tutorials!