How to use Twilio sms send api in PHP-Laravel
Step 1. First of all you need create an account on website www.twilio.com for use twilio send sms api.
Step 2:Now Get a phone number for free trial twilio
Step 3:Now Get Twilio Account SID and Auth token for use twilio sms api.
NOTE : you can only use one phone number in your twilio trial account and it should be verified by twilio.
How to use it in Laravel
Step 1: Create a new laravel project
Step 2:Run command in composer
composer require twilio/sdk
Step 3:Add these in your .env file
TWILIO_ACCOUNT_SID=ACXXXXXXXXXXXXXXXXXXX
TWILIO_AUTH_TOKEN=XXXXXXXXXXXXXXXXXXXX
TWILIO_APP_SID=APXXXXXXXXXXXXXXXXXXX
Step 4: Now add it in config/app.php.
'twilio' => [
'TWILIO_AUTH_TOKEN' => env('TWILIO_AUTH_TOKEN'),
'TWILIO_ACCOUNT_SID' => env('TWILIO_ACCOUNT_SID'),
'TWILIO_APP_SID' => env('TWILIO_APP_SID')
],
Step 5: Now create sms controller.
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Twilio\Rest\Client;
use Twilio\Jwt\ClientToken;
class SmsController extends Controller
{
public function sendSms()
{
$accountSid = config('app.twilio')['TWILIO_ACCOUNT_SID'];
$authToken = config('app.twilio')['TWILIO_AUTH_TOKEN'];
$appSid = config('app.twilio')['TWILIO_APP_SID'];
$client = new Client($accountSid, $authToken);
try
{
// Use the client to do fun stuff like send text messages!
$client->messages->create(
// the number you'd like to send the message to
'+919033999999',
array(
// A Twilio phone number you purchased at twilio.com/console
'from' => '+17204393482',
// the body of the text message you'd like to send
'body' => 'Hey Ketav! It’s good to see you after long time!'
)
);
}
catch (Exception $e)
{
echo "Error: " . $e->getMessage();
}
}
}
No comments:
Post a Comment