Get Started with PHP using Laravel Framework

    Sign up for a Plivo Account

    When you sign up with Plivo, you will start with a free trial account to experiment with and learn about our services. This free trial account comes with free credits. If you wish to continue with our service, you can add more credits and purchase a number by clicking here. Add a number and credits to your account to start testing the full range of our voice and SMS service features.

    Sign up here to get your free Plivo account today.

    Follow these steps to successfully sign up for a free trial account:

    1. Sign up with your work email address
    2. Check your inbox for an activation email from Plivo. Click on the link in the email to activate your account.
    3. Enter your mobile number to complete the phone verification step.

    Sign up with your work email address

    Sign up with email addres

    If you have any issues creating a Plivo account, please reach out to our Support Team for assistance.

    Start your implementation with Plivo to send/receive messages using PHLO or the traditional API/XML way. PHLO allows you to create the call flows using its intuitive canvas and deploy it with few clicks. Refer to the instructions from the respective tabs below to start your integration with PHLO or API as you wish.

    Send your First Outbound SMS/MMS

    To send your first outbound sms, you can create and deploy a PHLO in just a few clicks using the PHLO canvas. PHLO also lets you visually construct your entire use-case. With PHLO, only pay for sms you send/receive. Building with PHLO is free.

    Prerequisites

    1. Create a Plivo account (if you don’t have one already): Sign up with your work email address and complete the phone verification step using your mobile number.
    2. Set Up Your PHP Laravel Dev environment: To set up your dev environment in PHP Laravel, please refer to the instructions available in the Set Up Your PHP Laravel Dev Environment section.
    3. Buy a Plivo Number(optional): You must have an SMS-enabled Plivo phone number if you wish to receive incoming sms. Purchase numbers from the Numbers section of your Plivo Console. It is also possible to purchase numbers using the Numbers API.

    Create the PHLO

    Create a PHLO to send your first outbound SMS/MMS by following these instructions:

    • On the side navigation bar, click PHLO. The PHLO page will appear and display your existing PHLOs, if any exist. If this is your first PHLO, then the PHLO page will be empty.
    • Click Create New PHLO to build a new PHLO.
    • In the Choose your use-case window, click Build my own. The PHLO canvas will appear with the Start node.
      Note: The Start node is the starting point of any PHLO. You can choose between the four available trigger states of the start node; Incoming SMS, Incoming Call, and API Request. For this PHLO, we will use the API Request trigger state.
    • From the list of components on the left-hand side, drag and drop the Send Message component onto the canvas. This will add an Send Message node onto the canvas.
    • Connect the Start node with the Send Message node, using the API Request trigger state.
    • Configure the Send Message node with the source/from number using the From field. Enter the destination number you wish to send message in the To field.
    • If you’d like to send MMS then Configure the Media URLs node with the Media files you’d like to send using the Media URLs field.
    • Once you have configured the node, click Validate to save the configurations.
    • After you complete the configurations, provide a recognizable name for your PHLO and click Save. Your PHLO is now ready. You can trigger the PHLO and test it out. For more information, refer to the following section.

    Set Up Your PHP Laravel Dev Environment

    You must set up and install PHP, Laravel and Plivo’s PHP SDK to send your first outbound sms. Here’s how.

    Install PHP

    Operating SystemInstructions
    macOSYou can install PHP using the official installer. You can also install it from here.
    LinuxTo install PHP on Linux you can find the instructions here.
    WindowsTo install PHP on Windows you can use the official installer.

    Install Composer

    Composer is a dependency manager for PHP that is used in all modern PHP frameworks, such as Symfony and Laravel. We highly recommend using Composer as the package manager for your web project.

    1. Download the latest version of Composer.
    2. Run the following command in Terminal in order to run the composer:

       $ php ~/Downloads/composer.phar --version
      

      Note: PHAR (PHP archive) is an archive format for PHP that can be run on the command line

    3. Run the following command to make it executable:

       $ cp ~/Downloads/composer.phar /usr/local/bin/composer
       $ sudo chmod +x /usr/local/bin/composer
       $ Make sure you move the file to bin directory.
      
    4. To check if the path has /usr/local/bin, use

       $ echo $PATH
      

      If the path is different, use the following command to update the $PATH:

       $ export PATH = $PATH:/usr/local/bin
       $ source ~/.bash_profile
      

      Note: If your PATH doesn’t include /usr/local/bin directory, we recommend adding it so that you can access it globally.

    5. You can also check the version of Composer by running the following command:

       $ composer --version.       
      

    1. Run the following command:

       $ curl -sS https://getcomposer.org/installer | php
      
    2. Run the following command to make the composer.phar file as executable:

       $ chmod +x composer.phar
      

      Note: PHAR (PHP archive) is an archive format for PHP that can be run on the command line

    3. Run the following command to make Composer globally available for all system users:

       $ mv composer.phar /usr/local/bin/composer
      

    1. Download and run the Windows Installer for Composer.

      Note: Make sure to allow Windows Installer for Composer to make changes to your php.ini file.

    2. If you have any terminal windows open, close all instances and open a fresh terminal instance.
    3. Run the Composer command.

       $ composer -V
      

    Install Laravel & Create a Laravel Project

    • Use the below command to install Laravel:

      $ composer require laravel/installer
      

    As we have Laravel and its dependencies installed, we can use them to create a new Laravel project. As the initial step, using Laravel we can auto-generate code in the Laravel folder structure.

    • Change the directory to our project directory in the command line:

      $ cd mylaravelapp
      
    • Use the below command to start your Laravel project:

      $ composer create-project laravel/laravel quickstart --prefer-dist
      
    • To install the stable release, run the following command in the project directory:

      $ composer require plivo/plivo-php
      

    This will create a quickstart directory with the necessary folders & files for development.

    Install Plivo

    • To install a specific release, run the following command in the project directory:

      $ composer require plivo/plivo-php:4.15.0
      
    • Alternatively, you can download this source and run

      $ composer install
      

    This generates the autoload files, which you can include using the following line in your PHP source code to start using the SDK.

    <?php
    require 'vendor/autoload.php'
    

    Trigger the PHLO

    Once you have created and setup your PHP dev envrironment, you can go to your Plivo Consolse and copy the PHLO_ID. Integrate a PHLO into your application workflow by making an API request to trigger the PHLO with the required payload.

    Create a Laravel Controller

    Change the directory to our newly created project directory, i.e, quickstart directory and run the below command to create a Laravel controller for outbound sms.

    $ php artisan make:controller PhloController
    

    This will generate a controller named PhloController in the app/http/controllers/ directory.

    With Static Payload

    You can choose to either configure the mandatory params required for a PHLO while creating the PHLO itself or, you can pass the params as payload while triggering the PHLO from your app.

    Note: When you configure the params while creating the PHLO, the values are hardcoded and act as static payload.

    With Static Payload

    Code

    Now, You have to open the app/http/controllers/phloController.php file and add the following code:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    
    <?php
    namespace App\Http\Controllers;
    require '../../vendor/autoload.php';
    use Plivo\Resources\PHLO\PhloRestClient;
    use Plivo\Exceptions\PlivoRestException;
    
    class PhloController extends Controller
    {
        public function triggerPhlo()
        {
            $client = new PhloRestClient("YOUR_AUTH_ID", "YOUR_AUTH_TOKEN");
            $phlo = $client->phlo->get("YOUR_PHLO_ID");
            try {
                $response = $phlo->run();
                echo json_encode($response);
            } catch (PlivoRestException $ex) {
                echo json_encode($ex);
            }
        }
    }
    

    With Dynamic Payload

    To use dynamic values for the parameters, follow the below steps.

    • Select the StartNode - under API request, fill the Payload key as From & To and keep the values empty, then click validate & save.

    With Dynamic Payload

    • Use the liquid templating params while creating the PHLO and pass the values while triggering the PHLO.

    With Dynamic Payload

    Code

    Now, You have to open the app/http/controllers/phloController.php file and add the following code:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    
    <?php
    namespace App\Http\Controllers;
    require '../../vendor/autoload.php';
    use Plivo\Resources\PHLO\PhloRestClient;
    use Plivo\Exceptions\PlivoRestException;
    
    class PhloController extends Controller
    {
        public function triggerPhlo()
        {
            $client = new PhloRestClient("YOUR_AUTH_ID", "YOUR_AUTH_TOKEN");
            $phlo = $client->phlo->get("YOUR_PHLO_ID");
            try {
                $response = $phlo->run(["From" => "+14157778888", "To" => "+14157778889"]); // These are the fields entered in the PHLO console
                echo json_encode($response);
            } catch (PlivoRestException $ex) {
                echo json_encode($ex);
            }
        }
    }
    

    Get your Auth_ID and Auth_token from your dashboard AUTHID

    You can find the PHLO_ID on the PHLO Listing page. PHLO Listing

    Test and Validate

    Add a Route

    Now, to add a route for the outbound function in the PhloController class, open the routes/web.php file and add the below line at the end of the file:

    Route::match(['get', 'post'], '/triggerphlo', 'PhloController@triggerPhlo');
    
    Note: If you are using Laravel 8 then you need to use the fully qualified Class name for your Controllers. For example:
    Route::match(['get', 'post'], '/triggerphlo', 'App\Http\Controllers\PhloController@triggerPhlo');

    Save the file and use the below command to run your code.

    $ php artisan serve
    

    Your local development server will be started and you can test the laravel app for outbound sms via the URL http://127.0.0.1:8000/triggerphlo/.

    Note: If you are using a Plivo Trial account for this example, you can only send sms to phone numbers that have been verified with Plivo. Phone numbers can be verified at the Sandbox Numbers page.

    Receive Your First Inbound SMS/MMS

    Create and deploy a PHLO to receive your first inbound SMS and forward to your web server with a few clicks on the PHLO canvas. PHLO also lets you visually construct your entire use-case. With PHLO, only pay for messages you send/receive. Building with PHLO is free.

    Prerequisites

    1. Create a Plivo account (if you don’t have one already): Sign up with your work email address and complete the phone verification step using your mobile number.
    2. Buy a Plivo number: You must have a sms-enabled Plivo phone number to receive incoming calls. Purchase numbers from the Numbers section of your Plivo Console. It is also possible to purchase numbers using the Numbers API.
    3. PHLO Application: When you receive a call on a Plivo sms-enabled number, you can control the call flow by associating a PHLO application to that Plivo phone number. Plivo will fetch the PHLO associated with the number and expect valid instructions via PHLO to handle the sms.

    Create the PHLO

    You can create a PHLO by referring to the below instructions to receive your first incoming SMS/MMS:

    • On the side navigation bar, click PHLO. The PHLO page will appear and display your existing PHLOs, if any exist. If this is your first PHLO, then the PHLO page will be empty.
    • Click Create New PHLO to build a new PHLO.
    • In the Choose your use-case window, click Build my own. The PHLO canvas will appear with the Start node.
      Note: The Start node is the starting point of any PHLO. You can choose between the four available trigger states of the start node; Incoming SMS, Incoming Call, and API Request. For this PHLO, we will use the Incoming Message trigger state.
    • From the list of components on the left-hand side, drag and drop the HTTP Request component onto the canvas. This will add a HTTP Request node onto the canvas.
    • Connect the Start node with the HTTP Request node, using the Incoming Message trigger state.
    • Configure the HTTP Request node to send the details to your Web Server.
    • Once you have configured the node, click Validate to save the configurations.
    • After you complete the configurations, provide a recognizable name for your PHLO and click Save. Your PHLO is now ready.

    Assign the PHLO to a Plivo Number for Incoming SMS/MMS

    Once you have created and configured your PHLO, assign your PHLO to a Plivo number.

    To assign a PHLO to a number:

    1. Log in to your Plivo Console
    2. On the Product Navigation bar, click Phone Numbers.
    3. On the Numbers page, under Your Numbers, click the phone number you wish to use for the PHLO.
    4. In the Number Configuration window, select PHLO from the Application Type list.
    5. From the PHLO Name list, select the PHLO you wish to use with the phone number, and then click Update Number.

    Assign PHLO to a Plivo Number

    If you have not purchased a phone number yet, you can buy a phone number(s) by referring to the instructions available here.

    Test and Validate

    You can now send a message to your Plivo phone number to receive an incoming sms and see how the inbound sms is handled using PHLO.

    For more information about creating a PHLO app, see the PHLO User Guide.
    For information on components and their variables, see the PHLO Components Library.

    Forward an Incoming SMS/MMS

    To forward an incoming sms, you can create and deploy a PHLO with a few clicks on the PHLO canvas.

    Prerequisites

    1. Create a Plivo account (if you don’t have one already): Sign up with your work email address and complete the phone verification step using your mobile number.
    2. Buy a Plivo number: You must have a sms-enabled Plivo phone number to receive incoming sms. Purchase numbers from the Numbers section of your Plivo Console. It is also possible to purchase numbers using the Numbers API.
    3. PHLO Application: When you receive a sms on a Plivo sms-enabled number, you can control the flow by associating a PHLO application to that Plivo phone number. Plivo will fetch the PHLO associated with the number and expect valid instructions via PHLO to handle the sms.

    Create the PHLO

    You can create a PHLO by referring to the below instructions to forward an incoming SMS/MMS:

    • On the side navigation bar, click PHLO. The PHLO page will appear and display your existing PHLOs, if any exist. If this is your first PHLO, then the PHLO page will be empty.
    • Click Create New PHLO to build a new PHLO.
    • In the Choose your use-case window, click Build my own. The PHLO canvas will appear with the Start node.
      Note: The Start node is the starting point of any PHLO. You can choose between the four available trigger states of the start node; Incoming SMS, Incoming Call, and API Request. For this PHLO, we will use the Incoming Message trigger state.
    • From the list of components on the left-hand side, drag and drop the Send Message component onto the canvas. This will add a Send Message node onto the canvas.
    • Connect the Start node with the Send Message node, using the Incoming Message trigger state.
    • Configure the Send Message node with the phone number to which you want to forward the Message. For example, in this case, “+14157778889”.
    • Once you have configured the node, click Validate to save the configurations.
    • After you complete the configurations, provide a recognizable name for your PHLO and click Save. Your PHLO is now ready.

    Assign the PHLO to a Plivo Number to Forward Incoming Calls

    Once you have created and configured your PHLO, assign your PHLO to a Plivo number.

    To assign a PHLO to a number:

    1. Log in to your Plivo Console
    2. On the Product Navigation bar, click Phone Numbers.
    3. On the Numbers page, under Your Numbers, click the phone number you wish to use for the PHLO.
    4. In the Number Configuration window, select PHLO from the Application Type list.
    5. From the PHLO Name list, select the PHLO you wish to use with the phone number, and then click Update Number.

    Assign PHLO to a Plivo Number

    If you have not purchased a phone number yet, you can buy a phone number(s) by referring to the instructions available here.

    Test and Validate

    You can now send a sms to your Plivo phone number and see how the inbound sms is forwarded to the phone number specified in the PHLO.

    For more information about creating a PHLO app, see the PHLO User Guide.
    For information on components and their variables, see the PHLO Components Library.

    Install PHP, Laravel and the Plivo PHP Server SDK

    You must set up and install PHP, Laravel and Plivo’s PHP SDK to send your first SMS. Here’s how.

    Install PHP

    Operating SystemInstructions
    macOSYou can install PHP using the official installer. You can also install it from here.
    LinuxTo install PHP on Linux you can find the instructions here.
    WindowsTo install PHP on Windows you can use the official installer.

    Install Composer

    Composer is a dependency manager for PHP that is used in all modern PHP frameworks, such as Symfony and Laravel. We highly recommend using Composer as the package manager for your web project.

    1. Download the latest version of Composer.
    2. Run the following command in Terminal in order to run the composer:

       $ php ~/Downloads/composer.phar --version
      

      Note: PHAR (PHP archive) is an archive format for PHP that can be run on the command line

    3. Run the following command to make it executable:

       $ cp ~/Downloads/composer.phar /usr/local/bin/composer
       $ sudo chmod +x /usr/local/bin/composer
       $ Make sure you move the file to bin directory.
      
    4. To check if the path has /usr/local/bin, use

       $ echo $PATH
      

      If the path is different, use the following command to update the $PATH:

       $ export PATH = $PATH:/usr/local/bin
       $ source ~/.bash_profile
      

      Note: If your PATH doesn’t include /usr/local/bin directory, we recommend adding it so that you can access it globally.

    5. You can also check the version of Composer by running the following command:

       $ composer --version.       
      

    1. Run the following command:

       $ curl -sS https://getcomposer.org/installer | php
      
    2. Run the following command to make the composer.phar file as executable:

       $ chmod +x composer.phar
      

      Note: PHAR (PHP archive) is an archive format for PHP that can be run on the command line

    3. Run the following command to make Composer globally available for all system users:

       $ mv composer.phar /usr/local/bin/composer
      

    1. Download and run the Windows Installer for Composer.

      Note: Make sure to allow Windows Installer for Composer to make changes to your php.ini file.

    2. If you have any terminal windows open, close all instances and open a fresh terminal instance.
    3. Run the Composer command.

       $ composer -V
      

    Install Laravel & Create a Laravel Project

    • Use the below command to install Laravel:

      $ composer require laravel/installer
      

    As we have Laravel and its dependencies installed, we can use them to create a new Laravel project. As the initial step, using Laravel we can auto-generate code in the Laravel folder structure.

    • Change the directory to our project directory in the command line:

      $ cd mylaravelapp
      
    • Use the below command to start your Laravel project:

      $ composer create-project laravel/laravel quickstart --prefer-dist
      
    • To install the stable release, run the following command in the project directory:

      $ composer require plivo/plivo-php
      

    This will create a quickstart directory with the necessary folders & files for development.

    Install Plivo

    • To install a specific release, run the following command in the project directory:

      $ composer require plivo/plivo-php:4.15.0
      
    • Alternatively, you can download this source and run

      $ composer install
      

    This generates the autoload files, which you can include using the following line in your PHP source code to start using the SDK.

    <?php
    require 'vendor/autoload.php'
    

    Send your first Outbound SMS/MMS

    This section will guide you through how to use Plivo APIs to Send SMS from your application. First, let’s make sure you meet these prerequisites before we dive into the code.

    • Plivo Auth Id and Auth Token: You will find your Plivo Auth Id and Auth Token on the home screen of your Plivo Console. Click here to sign-up for a Plivo account if you haven’t already!

    Find Your Auth Credentials on Plivo Console

    • Plivo Phone Number: You must have a SMS-enabled Plivo phone number to send messages to the US and Canada. Purchase numbers from the Numbers section of your Plivo Console. It is also possible to purchase numbers using the Numbers API.

    Buy a New Plivo Number

    Create a Laravel Controller

    Change the directory to our newly created project directory, i.e, quickstart directory and run the below command to create a Laravel controller send an outbound SMS.

    $ php artisan make:controller SMSController
    

    This will generate a controller named SMSController in the app/http/controllers/ directory. Now, You have to open the app/http/controllers/SMSController.php file and add the following code:

    Code

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    
    <?php
    namespace App\Http\Controllers;
    require '../../vendor/autoload.php';
    use Plivo\RestClient;
    use Illuminate\Http\Request;
    
    class SMSController extends Controller
    {
        public function sendSMS()
        {
            $client = new RestClient("<auth_id>","<auth_token>");
            $response = $client->messages->create(
                '<Caller_ID>', 
                ['<Destination_Number>'], 
                'Hello, from PHP Laravel!'
            );
            header('Content-Type: application/json');
            echo json_encode($response);
        }
    }
    ?>
    
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    
    <?php
    namespace App\Http\Controllers;
    require '../../vendor/autoload.php';
    use Plivo\RestClient;
    use Illuminate\Http\Request;
    
    class SMSController extends Controller
    {
        public function sendMMS()
        {
            $client = new RestClient("<auth_id>","<auth_token>");
            $mediaURLs = ['https://media.giphy.com/media/26gscSULUcfKU7dHq/source.gif'];
            $mediaIDs = ['801c2056-33ab-499c-80ef-58b574a462a2'];
            $response = $client->messages->create(
                '<Caller_ID>',
                ['<Destination_Number>'],
                'Hello, MMS from PHP server!',
                ['type' => 'mms', 'media_urls' => $mediaURLs, 'media_ids' => $mediaIDs]
            );
            header('Content-Type: application/json');
            echo json_encode($response);
        }
    }
    ?>
    
    Note:
    • Replace the placeholders auth_id & auth_token with your credentials from yourPlivo Console.
    • We recommend that you store your credentials in the auth_id & auth_token environment variables, so as to avoid the possibility of accidentally committing them to source control. If you do this, you can initialize the client with no arguments and it will automatically fetch them from the environment variables
    • You can use $_ENV or putenv/getenv function to store environment variables and fetch them while initializing the client.
    • Replace the placeholder +14151234567 with the Phone number which you have purchased and +14157654321 with the phone number you will be sending sms to.
    • Both +14151234567 and +14157654321 should be in E.164 format

    Add a Route

    Now, to add a route for the outbound function in the SMSController class, open the routes/web.php file and add the below line at the end of the file:

    Route::match(['get', 'post'], '/sendSMS', 'SMSController@sendSMS');
    
    Note:
  • If you are using Laravel 8 then you need to use the fully qualified Class name for your Controllers. For example:
    Route::match(['get', 'post'], '/sendSMS', 'App\Http\Controllers\SMSController@sendSMS');
  • We have added the route of the app to the “except” array to disable CSRF verification - app/Http/Middleware/VerifyCsrfToken.php
  • Test & Validate

    Now the SMSController is ready for your first outbound sms, you can use the below command to initiate your first outbound using Laravel and Plivo PHP SDK.

    $ php artisan serve
    

    Your local development server will be started and you can test the laravel app for outbound sms via the URL http://127.0.0.1:8000/sendSMS/.

    Note: If you are using a Plivo Trial account for this example, you can only make calls to phone numbers that have been verified with Plivo. Phone numbers can be verified at the Sandbox Numbers page.

    Set up PHP laravel server for Incoming Messages & Callbacks.

    In this section, we’ll walk you through how to set up a Laravel server in under five minutes and start handling incoming calls & callbacks.

    Now, open the app/http/controllers/SMScontroller.rb file and add include the following code in the SMSController class after the sendSMS function:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    
    <?php
    
    namespace App\Http\Controllers;
    require '../../vendor/autoload.php';
    use Plivo\RestClient;
    use Plivo\XML\Response;
    use Illuminate\Http\Request;
    
    class VoiceController extends Controller
    {
        // To make your first outbound sms
        public function sendSMS()
        {
            .......
        }
    
       // Receive incoming SMS
        public function receivesms()
        {
             $from_number = $_REQUEST["From"];
             $to_number = $_REQUEST["To"];
             $text = $_REQUEST["Text"];
             echo("Message received - From $from_number, To: $to_number, Text: $text");
        }
    }
    
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    
    <?php
    
    namespace App\Http\Controllers;
    require '../../vendor/autoload.php';
    use Plivo\RestClient;
    use Plivo\XML\Response;
    use Illuminate\Http\Request;
    
    class VoiceController extends Controller
    {
        // To make your first outbound sms
        public function sendSMS()
        {
            .......
        }
    
       // Receive incoming SMS
        public function receivesms()
        {
             $from_number = $_REQUEST["From"];
             $to_number = $_REQUEST["To"];
             $text = $_REQUEST["Text"];
             $media_url = $_REQUEST["Media0"];
             echo("Message received - From $from_number, To: $to_number, Text: $text, Media: $media_url");
        }
    }
    

    Add a Route

    Now, you need to add a route for the receivesms function in SMSController class, open the routes/web.php file and add the below line after the sendSMS route:

    Route::match(['get', 'post'], '/sendSMS', 'SMSController@sendSMS');
    Route::match(['get', 'post'], '/receivesms', 'SMSController@receivesms');
    

    Test and validate

    Test & Validate

    Now the SMSController is ready for your first inbound sms, you can use the below command to receive your first inbound using Laravel and Plivo PHP SDK.

    $ php artisan serve
    

    Your local development server will be started and you can test the laravel app for inbound sms via the URL http://127.0.0.1:8000/receivesms/.

    Exposing your local server to the internet

    To receive Incoming SMS and to handle callbacks, your local server should be able to connect with the Plivo API service. Ngrok is a tunneling software used to expose a web server running on your local machine to the internet. Using Ngrok you can set webhooks that can talk to the Plivo server.

    ngrok block diagram

    You can download and install ngrok from here. Follow the detailed configuration instructions to get started.

    Run ngrok on the port which currently hosts your application. For example, if your port number is 8000, run the following command:

    $ ./ngrok http 8000
    

    This will give you a UI with links that look like ngrok.io/* which you can use to access your local server using the public network.

    Sample ngrok CLI

    Create an Application

    1. Create an Application by visiting the Application Page and click on New Application. You can also use Plivo’s Application API.
    2. Give your application a name. Let’s call it Receive SMS. Enter your server URL (e.g. http://example.com/receive_sms/) in the Message URL field and set the method as POST. See our Application API docs to learn how to modify your application through our APIs.
    3. Click on Create Application to save your application.

    Create Application

    Assign a Plivo number to your app

    1. Navigate to the Numbers page and select the phone number you want to use for this app.
    2. Select Receive SMS (name of the app) from the Plivo App dropdown list.
    3. Click on Update Number to save.

    Assign Phone Number to Receive SMS App

    Test and validate

    Send an SMS to your Plivo number using a regular mobile phone. Plivo will send a request to your Message URL with the parameters listed in the Messages Documentation.

    Reply to an incoming SMS/MMS

    When an SMS is sent to an Plivo phone number, you can receive the text on your server by setting a Message URL in your Plivo app. Plivo will send the message along with other parameters to your Message URL. You can reply back using the Plivo Message XML.

    Note: In order to reply to a US or Canadian phone number, the src (source) phone number must be a Plivo US or Canadian phone number that is SMS-enabled. You can purchase a Plivo number from the Buy Numbers tab on your Plivo Console and filter the Phone Numbers by selecting SMS in the features option to buy SMS enabled numbers.

    Update the Laravel Controller

    Now, open the app/http/controllers/SMSController.php file and add include the following code in the SMSController class after the receivesms function:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    
    <?php
    namespace App\Http\Controllers;
    require '../../vendor/autoload.php';
    use Plivo\RestClient;
    use Plivo\XML\Response;
    use Illuminate\Http\Request;
    
    class VoiceController extends Controller
    {
        // To make your first outbound sms
        public function sendSMS()
        {
            ...... . .
        }
    
        // Receive incoming SMS
        public function receivesms()
        {
            ......... .
        }
    
        // Reply to incoming SMS
        public function replysms()
        {
            $number = $_POST["From"];
            $response = new Response();
            $params = array(
                'src' => "+14151234567",
                'dst' => $number,
                'callbackUrl' => "https://www.foo.com/sms_status/",
                'callbackMethod' => "POST"
            );
            $message_body = "Testing reply to an incoming SMS";
            $response->addMessage($message_body, $params);
    
            Header('Content-type: text/xml');
            return $response->toXML();
        }
    }
    

    Add a Route

    Now, you need to add a route for the inbound function in SMSController class, open the routes/web.php file and add the below line receivesms:

    Route::match(['get', 'post'], '/sendSMS', 'SMSController@sendSMS');
    Route::match(['get', 'post'], '/receivesms', 'SMSController@receivesms');
    Route::match(['get', 'post'], '/replysms', 'SMSController@replysms');
    
    Note: Create an application in the Plivo console with your Server URL in the answer_url field and assign the app to a Plivo number.

    Test & Validate

    Now the SMSController is ready to reply the incoming sms to your Plivo number, you can use the below command to start the Laravel server to reply incoming messages and to handle callbacks.

    $ php artisan serve
    

    Your local development server will be started and you can test the laravel app reply incoming messages via the URL http://127.0.0.1:8000/replysms/.

    Send an SMS to your Plivo number using a regular mobile phone and an automatic response will be sent from your Plivo number to your mobile phone. Also, Plivo will send a request to your Message URL with the parameters listed in the Messages Documentation.