TWILIO SMS VERIFICATION

SMS APIs

There are several SMS APIs available for PHP. Here are some popular ones:

  • Twilio

  • Nexmo

  • Plivo

  • Clickatell

Below take Twilio as a example. Twilio is a cloud communications platform that provides an SMS API that can be used with PHP. It offers features like SMS/MMS messaging, phone number lookup, and more.

Installation

Sign up for a Twilio account

If you haven't already, create a Twilio account.

Obtain your Twilio credentials: Once you have created an account, you will need to obtain your Account SID and Auth Token from the Twilio Console. You can find them on the dashboard.

To interact with the Twilio API in PHP, you will need to install the Twilio PHP library using Composer

Composer

You can use Composer or simply Download the Release. Follow the installation instructions if you do not already have composer installed.

Once composer is installed, execute the following command in your project root to install this library:

require __DIR__ . '/vendor/autoload.php';

Finally, be sure to include the autoloader at sendsms.php:

require __DIR__ . '/vendor/autoload.php';

Implementation on Your Website

sendsms.php


require __DIR__ . '/vendor/autoload.php';
session_start();

use Twilio\Rest\Client;

// Your Account SID and Auth Token from twilio.com/console
$account_sid = 'ACae07792961ca8cef99465b15e8c02a6c';
$auth_token = '1e2ee15327e5db8acfcfded0d985d784';
// In production, these should be environment variables. E.g.:
// $auth_token = $_ENV["TWILIO_AUTH_TOKEN"]

// Generate OTP
$OTP = mt_rand(100000, 999999);
$_SESSION['OTP'] = $OTP;

// A Twilio number you own with SMS capabilities
$twilio_number = "+15075955903";
$client = new Client($account_sid, $auth_token);
$client->messages->create(
    // Where to send a text message (your cell phone?)
    '+60176928902',
    array(
        'from' => $twilio_number,
        'body' => $OTP
    )
);

Last updated