Send SMS using Twilio SDK in PHP - Download Complete Code

Send SMS using Twilio SDK in PHP - Download Complete Code

What is Twilio?

Twilio allows us to send and receive Text SMS with the help of many programming languages which can use for multiple purposes like customer mobile verification and internet calling and virtual numbers, Video calls SDK, Voice call SDK, Twilio also allows users to make phone calls and receive phone calls with the help of web service APIs. Here we are going to use Twillo PHP SDK to send an SMS that can be used for OTP- Mobile number verification, any notification, or many more thing as per business need.

Create an Account on twilio.com

Let's begin the process to integrate Twilio SDK, Firstly you need to create an account on https://www.twilio.com/try-twilio. You will get a free trial in the form of a credit amount which is $10 to $15. 
You can upgrade your account later or instantly by adding business information & payment information on https://www.twilio.com/console/billing/upgrade

Get Twilio account SID and AUTH TOKEN

After completing the signup process visit the Twilio console "https://www.twilio.com/console" to get your account SID and Auth Token, which are required to make API calls.

Twilio console for ACCOUNT SID and AUTH TOKEN

Install Twilio SDK 

Use composer to download the Twilio PHP helper library using this command in CMD or shell command.

composer require twilio/sdk

After running this command on cmd you will get the confirmation

E:\xampp\htdocs\twilio\demo>composer require twilio/sdk
No composer.json in current directory, do you want to use the one at E:\xampp\htdocs\practice\twilio? [Y,n]? n
Using version ^6.23 for twilio/sdk
./composer.json has been created
Running composer update twilio/sdk
Loading composer repositories with package information
Updating dependencies
Lock file operations: 1 install, 0 updates, 0 removals
  - Locking twilio/sdk (6.23.0)
Writing lock file
Installing dependencies from lock file (including require-dev)
Package operations: 1 install, 0 updates, 0 removals
  - Downloading twilio/sdk (6.23.0)
  - Installing twilio/sdk (6.23.0): Extracting archive
1 package suggestions were added by new dependencies, use `composer suggest` to see details.
Generating autoload files

E:\xampp\htdocs\twilio\demo>

You will get the vendor folder, composer.lock, and composer.json files.

Note: If you don't have a composer in your system or don't know how to use composer, I am going to provide complete code to download so you can use downloaded code to send SMS by just putting account SID, Auth Token, and Twilio number.

Now we are going to implements SMS message delivery using the Services_Twilio class.

<?php
include("vendor/autoload.php");
$sid = "Your Account SID from www.twilio.com/console"; // Your Account SID from www.twilio.com/console
$token = "Your Auth Token from www.twilio.com/console"; // Your Auth Token from www.twilio.com/console
use Twilio\Rest\Client;
$client = new Twilio\Rest\Client($sid, $token);
$message = $client->messages->create(
  '+91XXXXXXXXXXX', // Text this number
  [
    'from' => '+1XXXXXX', // From a valid Twilio number
    'body' => 'Hello from Prashantabhishek.com!'
  ]
);

print $message->sid;

After running this code you will get a successful transaction id.

Read More

Create Dynamic Subdomain with the help of PHP and Htaccess

Send an Email Using Phpmailer and Amazon SES in PHP with Source Code

How to Create Dynamic Modal Box with Ajax, PHP and Mysql