Posted on / by Vivan Web Solution / in PHP Development

How to send SMS using PHP

In this tutorial, we have explained how to send SMS using PHP. There are many SMS API service providers like Twilio, Nexmo, MSG91, Text local that you can use to send SMS using the PHP programming language. In this tutorial we learned how to send SMS using twilio API service provider.

Twilio’s APIs (Application Programming Interfaces) power its platform for communications. Behind these APIs is a software layer connecting and optimizing communications networks around the world to allow your users to call and message anyone, globally. Twilio has a whole host of APIs, from SMS to Voice to Wireless!

Step 1: Register for Twilio account

Sign Up for free twilio account from here : https://www.twilio.com/try-twilio

Step 2: Verify Phone Number

Next, we need to create a phone number for your account from which you can send the SMS

  1. Click -> “Phone Numbers” in the sidebar
  2. Click -> “Get a number now” link to generate your own phone number
  3. Now “Click -> Get your first Twilio phone number”.
  4. Once you click “Get your first Twilio phone number”, a new pop-up window will show your Twilio Phone Number. Now, click “Choose this Number”.

Step 3: Create API Credentials

Next, you need to get your account SID and your authorization token.
On the Console Dashboard page you can get your credentials.

Step 4: Install PHP SDK

The method of installing the SDK is via composer

composer require twilio/sdk

The another method of installing the SDK without composer is possible to download and use the PHP SDK manually and unzip SDK folder into your project directory. You can download from here.

Step 7: Create Integration

<?php

require __DIR__ . '/vendor/autoload.php'; //with composer
// based on where you downloaded and unzipped the SDK
require __DIR__ . '/twilio-php-main/src/Twilio/autoload.php'; //without composer
use Twilio\Rest\Client;

// Your Account SID and Auth Token from twilio.com/console
$account_sid = 'ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX';
$auth_token = 'your_auth_token';

// In production, these should be environment variables. E.g.:
// $auth_token = $_ENV["TWILIO_AUTH_TOKEN"]

// A Twilio number you own with SMS capabilities
$twilio_number = "+15558675309";

$client = new Client($account_sid, $auth_token);
$client->messages->create(
    // Where to send a text message (your cell phone?)
    '+15017250604',
    array(
        'from' => $twilio_number,
        'body' => 'My first trial SMS!'
    )
);

Step 5: Receiving a SMS

Here you go!

Note: Trial accounts cannot send messages to unverified numbers; verify +9198XXX42887 at twilio.com/user/account/phone-numbers/verified, or purchase a Twilio number to send messages to unverified numbers.

Conclusion: I hope this tutorial helpful for you, if you have any issue with integration, please comment below.

Thank You!

Leave a Reply