Integrate Twilio SMS Service in a Website using PHP

in #utopian-io6 years ago (edited)

What Will I Learn?
Now we are going to learn how to setup and use Twilio for sending text messages to single/multiple recipients.

Requirements
PHP Server
Basic knowledge about PHP

Difficulty
Basic

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:

<?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( , ).

<?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.

<?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:&nbsp;&nbsp;&nbsp;<input name="receiver" type="text" /><br /><br />
Message:&nbsp;&nbsp;<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.

<?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";
  }
  
}



Posted on Utopian.io - Rewarding Open Source Contributors

Sort:  

Your contribution cannot be approved because it does not follow the Utopian Rules.

You can contact us on Discord.
[utopian-moderator]

Congratulations @coderlovely! You received a personal award!

Happy Birthday! - You are on the Steem blockchain for 1 year!

Click here to view your Board

Do not miss the last post from @steemitboard:

Carnival Challenge - Collect badge and win 5 STEEM
Vote for @Steemitboard as a witness and get one more award and increased upvotes!

Congratulations @coderlovely! You received a personal award!

Happy Birthday! - You are on the Steem blockchain for 2 years!

You can view your badges on your Steem Board and compare to others on the Steem Ranking

Do not miss the last post from @steemitboard:

Use your witness votes and get the Community Badge
Vote for @Steemitboard as a witness to get one more award and increased upvotes!

Coin Marketplace

STEEM 0.16
TRX 0.16
JST 0.030
BTC 57889.68
ETH 2457.18
USDT 1.00
SBD 2.40