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:
- Create a Twilio Account.
- Get SID and Token from Twilio.
- Set up Twilio PHP SDK using composer.
- 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:
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 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:
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__ . 'vendortwiliosdkTwilioautoload.php';
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!
//the number you'd like to send the message to
'TO PHONE NUMBER',
//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!
6 thoughts on “Twilio PHP API for sending SMS-OTP Tutorial & Example.”
I am glad that I came here and I can gain more information about this. You have written this article very factual which is understandable for everyone, Thanks for share it. bulk sms gateway usa
PHP essentials Pretty good post. I just stumbled upon your blog and wanted to say that I have really enjoyed reading your blog posts. Any way I'll be subscribing to your feed and I hope you post again soon. Big thanks for the useful info.
Fatal error: Uncaught TwilioExceptionsEnvironmentException: OpenSSL SSL_connect: Connection was reset in connection to api.twilio.com:443. Any help would be greatly appreciated.
And I am running SSL (https) on my site so that can't be the problem.
Unless it has to do with Open SSL, version, etc.
NFL Week 14 Odds – Vntopbet 메리트카지노 메리트카지노 カジノ シークレット カジノ シークレット 828Soccer24 Prediction for Tomorrow | Betting Tips & Odds