Welcome to ahex technologies

How To Send SMS Using Plivo and Laravel

Send SMS Using Plivo And Laravel

Short Message Service (SMS) is a text messaging service component of most telephone, World Wide Web, and mobile device systems. It has achieved huge success in the wireless world. Billions of SMS messages are sent every day. SMS is now a major revenue generator for wireless carriers. A lot of innovative applications are now built on top of the SMS technology and more are being developed. Many people are turning to short messaging service or SMS messaging as a way to communicate with other individuals instead of making phone calls.

While working on one of the applications being developed on the Laravel framework, I came across a situation where I had to send OTP on user’s mobile phone to verify user mobile number. This could easily be done using plivo. This package enables to send any kind of message to any mobile. So, this blog will provide you information about how to send SMS to any mobile device.

WHAT IS PLIVO

  • Plivo is a Cloud API Platform and a Global Carrier Services Provider for Voice Calls and SMS.
  • Their mission is to simplify global telecom and enable access to quality cloud communications at a low cost. This means that you can add voice calling and SMS text messaging capabilities to any web or mobile app with just a few lines of code.
  • This uses Plivo! API.
  • It requires AuthId and AuthToken, they can be generated by registering @at Plivo after registrion click on Dashboard ,there you will be able to see authid and authtoken.

sample snapshot:
picture

INSTALLATION

To install this package you will need:

  • Laravel 4 or 5
  • PHP

To install via composer run the following command from the terminal.

  • composer require lakshmaji/plivo

LARAVEL INTEGRATION

Once this has finished, you will need to add the service provider to the providers array in your app.php config as follows:

  • LakshmajiPlivoPlivoServiceProvider::class,

Next, also in the app.php config file, under the aliases array, you are required to add the Plivo facade.

  • ‘plivo’ => LakshmajiPlivoFacadePlivo::class,

Finally, you will want to publish the config using the following command:

  • php artisan vendor:publish

In the plivo.php configuration file we need to enter the Plivo API key and ID.

sample snapshot:

Capture

NOTE : Don’t forget to set a auth id and auth secret keys in the config file!, you can get them at Plivo dashboard.

EXAMPLE CODE TO SEND SMS IN PHP

require 'vendor/autoload.php';
use PlivoRestAPI;
$auth_id = "Your AUTH_ID";
$auth_token = "Your AUTH_TOKEN";

$p = new RestAPI($auth_id, $auth_token);

// Set message parameters
$params = array(
        'src' => '1111111111', // Sender's phone number with country code
        'dst' => '2222222222', // Receiver's phone number with country code
        'text' => 'Hi, Message from Plivo', // Your SMS text message
        'url' => 'http://example.com/report/', // The URL to which with the status
          of the message is sent
        'method' => 'POST' // The method used to call the url
       );
// Send message
$response = $p->send_message($params);

// Print the response
echo "Response : ";
print_r ($response['response']);

// Print the Api ID
echo " Api ID : {$response['response']['api_id']}";

// Print the Message UUID
echo "Message UUID : {$response['response']['message_uuid'][0]}";

EXAMPLE CODE TO SEND SMS IN LARAVEL USING CURL

use Plivo;    
public function sendSms()
{    
    $auth_id = "Your AUTH_ID";
    $auth_token = "Your AUTH_TOKEN";

    $src = '1111111111'; //Sender's phone number with country code
    $des = '2222222222'; // Receiver's phone number with country code
    $text = 'Hi, Message from Plivo'; // Your SMS text message
    
    $url = 'http://example.com/report/'; // The URL to which with the status of
           the message is sent
    $data_sms = array("src" => "$src", "dst" => "$dst", "text" => "$text");
    $data_string = json_encode($data_sms);

        $ch = curl_init($url);
            curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
            curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
            curl_setopt(   $ch, CURLOPT_CUSTOMREQUEST, "POST");
            curl_setopt($ch, CURLOPT_POSTFIELDS,$data_string);
            curl_setopt($ch, CURLOPT_USERPWD, $AUTH_ID . ":" . $AUTH_TOKEN);
            curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type:application/json'));
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

        $response = curl_exec($ch);
        if(!$response){
           die('Error: "' . curl_error($ch) . '" - Code: ' . curl_errno($ch));
          }

        $arr = json_decode($response, true );
        return $arr;

}