Now you can Subscribe using RSS

Submit your Email

Friday

Twilio PHP API for sending SMS-OTP Tutorial & Example.

Vishal Shrestha
Twilio provides a set of web APIs that enabled users to use standard web languages to build VoIP, voice and SMS apps via a web APIs.
Twilio has grown a lot and has a lot of APIs that can be used by customers to enhance the service that they are providing. Some of those APIs are Programmable SMS, Chat, Verification Services, Authentication, Elastic SIP Trunking and a lot more. Today we will be looking at Twilio's SMS API. We will send verification SMS to any required device using Twilio's SMS API. We will be using Twilio's PHP SDK . We can easily install it using Composer. It can easily be used without composer but composer pulls all the required libraries, dependencies and manage them all in one place. It's very useful and easy to learn. Learning it will help you manage a lot of dependencies. Now a good time to start learning it. Here's more details if you want to learn more about it, however for this project the details provided in this article will be enough. So lets begin. The steps involved in integrating Twilio SMS for our app are:
  1. Create a Twilio Account.
  2. Get SID and Token from Twilio.
  3. Set up Twilio PHP SDK using composer.
  4. Create a Twilio Client using PHP to send SMS.
You can check out the full tutorial video too :

Now lets go over the steps in detail:

1. Creating a Twilio Account
 If you don't already have an account, you need to sign up for using Twilio APIs. The Login screen looks like this:

Twilio-login-page
Twilio Login

Enter you name, password and select I'm just exploring in the question where you need to select which product do you plan to use. Select phone verification for the question "What are you building", select PHP as the language and finally select not a production app for the "Potential Monthly Interaction" question. After that you need to enter your phone number so that they can verify you. Enter you real phone number as a code will be sent to you for verification. Enter the code that you receive, you are now required to create a project name. Name the project SmsVerification, and click done. The account creation is now complete, you should be able to see your project console.

2. Get SID and Token from Twilio
You can see you Account SID and Auth Token on the first visible page in the console. The console looks like the image below, you can see the SID and Auth token blurred.

Twilio-console
Twilio Account Dashboard

 Once you have your Account SID and Auth token, next we will set up the Twilio PHP SDK.

3. Setting up Twilio PHP SDK
Now we will use composer to set up Twilio's PHP SDK. First of all download and install COMPOSER. After the composer has been successfully installed, create a project directory/folder if you haven't already. Open terminal and go to the root directory of you project and enter the following commands in the terminal:

composer init --require=twilio/sdk

After you hit enter, you'll be asked several things like author, minimum stability and some other project information. You can leave it as it is and just press enter through the options until it asks do you want to resolve dependencies interactively? You need to type n meaning no for that option.
This will create a composer.json file with the required details for installing Twilio's PHP SDK. Now run the following command:

composer install

You'll be able to see Twilio SDK being downloaded and set up. After the process has been successfully installed, there will be a vendor directory created with a twilio directory with several other files and directories inside it. Now we are ready to code.

4. Creating a Twilio Client using PHP to send SMS
 In your PHP file put the following statements in the beginning of the code:

require __DIR__ . '\vendor\twilio\sdk\Twilio\autoload.php';
use Twilio\Rest\Client;

For sending the message, we need to create a client object defined in the Twilio SDK, here the required code:

    $sid = 'YOUR ACCOUNT SID';
    $token = 'YOUR AUTH TOKEN';
    $client = new Client($sid, $token);

    //Use the client to do fun stuff like send text messages!
    $client->messages->create(
    //the number you'd like to send the message to
        'TO PHONE NUMBER',
        array(
        //A Twilio phone number you purchased at twilio.com/console
            'from' => 'FROM PHONE NUMBER',
            'body' => 'TEST MESSAGE'
        )
    );
 

NOTE: The from is a phone number that you will need to purchase in your twilio.com/console. Remember that you cannot send SMS with test SID and AUTH token even if you have purchased a phone number. You need actual production sid and token with the number that you bought for the SMS to be sent to an actual number.
That's it, this is all that is required for using the Twilio API for message sending, you can use this for OTP verification, SMS notifications and much more. It's pretty straight forward, if you have any problems feel free to put them in the comment section below. Happy Coding!

Vishal Shrestha / Author & Founder

A developer by profession, a born Adventurer. I mainly do Android but like to get my hands dirty with web development and a little bit of Python. I would't rather go on a Trek than a party and you can find me having a few rounds with the heavy bag to let out the steam ;)

For Business info : My Portfolio Site.

0 comments:

Post a Comment

Coprights @ 2017 | The Code City by Vishal Shrestha Vishal Shrestha