KodeInfo | Learning resources for web and mobile development

SENDING SMS USING TWILIO

September 4th, 2014 22:36:43 by Imran Iqbal Comments(0) - Views(12742)

Ever wondered how to send SMS message through Laravel App , here i am for your rescue . Today we will use Twilio a Cloud Based Communication System some of the features  includes call routing , sms , mms , SIP , call recordings . Everything in Twilio can be done using its nice and powerful API . We will use twilio to create our SMS sending app .

 
 

 

 

SETTING UP TWILIO

Go to twilio and click on Sign Up for Free . Fill out your details and click Get Started . Now Twilio will ask you mobile no which will be used to verify your account if sms verification do not work then click on call to verify your account . Once verified you will be redirected to Twilio tour after that go to dashboard you will see two fields ACCOUNT SID and AUTH TOKEN this are used to validate your app with Twilio Server . To reveal the AUTH TOKEN click on Lock Icon .  Click on Configure your Trial number on dashboard to see your configured trial number . If you are using trial account you cannot send sms message to unverifed numbers . To see all verified numbers click here , If there are no numbers in the list then add a new number . We will use our Twilio trial number to send sms message to one of our verified number .  We will use official Twilio package for Laravel

Open composer.json and add below lines to require array

"twilio/sdk": "dev-master"

Now run

composer update

VIEWS

We will use bootstrap to style our message field and Send button . We will have only one view which will have a text field to enter message and a button to send the message .

views/index.blade.php

<html>
    <head>
        <script src="//code.jquery.com/jquery-1.11.0.min.js"></script>
        <script src="//code.jquery.com/jquery-migrate-1.2.1.min.js"></script>
        <!-- Latest compiled and minified CSS -->
        <link rel="stylesheet" href="//netdna.bootstrapcdn.com/bootstrap/3.1.1/css/bootstrap.min.css">
 
        <!-- Optional theme -->
        <link rel="stylesheet" href="//netdna.bootstrapcdn.com/bootstrap/3.1.1/css/bootstrap-theme.min.css">
 
        <!-- Latest compiled and minified JavaScript -->
        <script src="//netdna.bootstrapcdn.com/bootstrap/3.1.1/js/bootstrap.min.js"></script>
 
    </head>
    <body>
 
        <div class="container" style="margin-top:100px;">
            <div class="row">
                {{Form::open(array("url"=>"/send"))}}
                <div class="col-xs-6">
                    <input type="text" class="form-control" name="message"/>
                </div>
                <div class="col-xs-6">
                    <button class="btn btn-success" type="submit">Send</button>
                </div>
                {{Form::close()}}
            </div>
        </div>
 
    </body>
</html>

 

ROUTES

As you can see in index.blade.php we are submitting form to /send . So we need two routes , first to display index page and second to send message .

Route::get('/', 'SMSController@index');
Route::post('/send', 'SMSController@send');

CONTROLLERS

Lets now create our SMSController . Our SMSController code is below

<?php
class SMSController extends BaseController {
 
    public function __construct() {
 
    }
 
    public function index() {
        return View::make('index');
    }
 
    public function send() {
        if (Input::has("message")) {
            $sid = "YOUR ACCOUNT SID";
            // Your Account SID from www.twilio.com/user/account
            $token = "YOUR ACCOUNT AUTH TOKEN";
            // Your Auth Token from www.twilio.com/user/account
 
            $client = new Services_Twilio($sid, $token);
            $message = $client -> account -> messages -> sendMessage('YOUR TRIAL/PAID TWILIO NUMBER', // From a valid Twilio number
            'YOUR VERIFIED NUMBER', // Text this number
            Input::get("message"));
 
            echo "Message Send Successfully Message SID :: " . $message -> sid;
        } else {
            echo "Error : No Message Entered <a href='/'>click here</a> to go back";
        }
    }
 
}

If everything is correct then you will see below message , for you SID will be different .

To check logs click on Logs in menu and click on any submenu like messages etc . Since we have sent a message output is shown below .

Feel free to comment if you have any doubt .

Thanks

KodeInfo Team

Author

  • Imran Iqbal
    Imran Iqbal

    Imran is a web developer and consultant from India. He is the founder of KodeInfo, the PHP and Laravel Community . In the meantime he follows other projects, works as a freelance backend consultant for PHP applications and studies IT Engineering . He loves to learn new things, not only about PHP or development but everything.

Related

WHY USE A FRAMEWORK OVER PLAIN PHP

WHY USE A FRAMEWORK OVER PLAIN PHP
read more

GETTING STARTED WITH LARAVEL

GETTING STARTED WITH LARAVEL
read more

UNDERSTANDING LARAVEL STRUCTURE

UNDERSTANDING LARAVEL STRUCTURE
read more

UNDERSTANDING LARAVEL ROUTES

UNDERSTANDING LARAVEL ROUTES
read more

comments powered by Disqus