Set Up a PHP Dev Environment for Messaging

This guide shows how to set up a development environment in five minutes to trigger API requests related to our Messaging API. It also shows how to send and receive messages using tunneling software to expose the local dev server to the public internet.

Install PHP, Composer, Laravel, and the Plivo PHP SDK

To get started, install PHP, the Composer dependency manager, the Laravel web framework, and Plivo’s PHP SDK.

Install PHP

Operating System Instructions
macOS Install PHP using the official macOS installer or by downloading and installing it.
Linux Download and install PHP using your favorite package installer.
Windows Use the official Windows installer.

Install Composer

All modern PHP frameworks use the Composer dependency manager; we highly recommend using it as the package manager for your web project. Follow the instructions to download and install Composer for macOS and Linux and for Windows, or follow the steps below.

  1. Download the latest version of Composer.
  2. Run this command in Terminal:

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

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

  3. Copy the file to /usr/local/bin and make it executable:

     $ cp ~/Downloads/composer.phar /usr/local/bin/composer
     $ sudo chmod +x /usr/local/bin/composer
    
  4. If your PATH doesn’t include /usr/local/bin directory, we recommend adding it so that you can access it globally. To check if the path has /usr/local/bin, enter

     $ echo $PATH
    

    If necessary, run these commands to update the $PATH:

     $ export PATH = $PATH:/usr/local/bin
     $ source ~/.bash_profile
    
  5. You can also check the version of Composer by running this command:

     $ composer --version.       
    

  1. Run the command

     $ curl -sS https://getcomposer.org/installer | php
    
  2. Make the composer.phar file 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 this 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: 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 and create a Laravel project

Install the Laravel web framework by running the command

$composer require laravel/installer

Once you have Laravel installed, create a project directory using the command mkdir mylaravelapp, then change to that directory and create a new Laravel project.

  composer create-project laravel/laravel quickstart --prefer-dist

This command creates a directory named quickstart with the necessary folders and files for development.

Install the PHP Plivo SDK

To install the stable release of the Plivo SDK, run this command in the project directory:

composer require plivo/plivo-php

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

composer require plivo/plivo-php:4.15.0

Alternatively, you can download source code from GitHub and run

composer install

The composer command generates the autoload files, which you can include in your PHP source code by using the line

<?php
require 'vendor/autoload.php'

Create a Laravel controller to trigger an API request

Now you can create a file in the project directory and execute code to trigger any Plivo API. Here’s some example code that sends an SMS message. Change to the quickstart directory and run this command to create a Laravel controller to send an outbound SMS message.

php artisan make:controller SMSController

This command generates a controller named SMSController in the app/http/controllers/ directory. Edit the app/http/controllers/SMSController.php file and paste into it this code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
<?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(
            [  
                "src" => "<sender_id>",
                "dst" => "<destination_number>",
                "text"  =>"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
25
26
27
28
<?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(
            [  
                "src" => "<sender_id>",
                "dst" => "<destination_number>",
                "text"  =>"Hello, from Laravel!",
                "type" => "mms", 
                "media_urls" => $mediaURLs, 
                "media_ids" => $mediaIDs
             ]
        );
        header('Content-Type: application/json');
        echo json_encode($response);
    }
}
?>
Note:
       
  • Replace the placeholders <auth_id> and <auth_token> with your authentication credentials, which you can find on the overview page of the Plivo console.
  • We recommend that you store your credentials in the auth_id and auth_token environment variables 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 the values from the environment variables.
  • You can use $_ENV or putenv/getenv to store environment variables and fetch them while initializing the client.
  • Replace the placeholder <Caller_ID> with a phone number you’ve purchased, and <Destination_Number> with the phone number you’ll be calling. Both phone numbers should be in E.164 format.

Add a route

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

Route::match(['get', 'post'], '/sendSMS', 'SMSController@sendSMS');
Note:
  • If you’re using Laravel 8, you need to use a fully qualified class name for your controllers. For example:
    Route::match(['get', 'post'], '/sendSMS', 'App\Http\Controllers\SMSController@sendSMS');
  • We added the route of the app to the “except” array to disable CSRF verification — app/Http/Middleware/VerifyCsrfToken.php

Now the SMSController is ready. Use this command to initiate an outbound SMS message.

php artisan serve

Your local development server will be started and you can test the application for outbound messaging via the URL http://localhost:8000/sendSMS/.

You can follow the same approach to trigger other API requests. Refer to our detailed API reference to see all the API requests available on the Messaging API platform.

Set up a Laravel server to serve XML and manage callbacks

Now that we’ve sent a message, let’s set up a Laravel server to handle incoming messages.

Plivo supports receiving SMS text messages in several countries (see complete SMS API coverage). When someone sends a text message to a Plivo phone number, you can receive it on your server by setting a Message URL in your Plivo application. Plivo sends the message, along with other parameters, to your Message URL.

Edit app/http/controllers/SMScontroller.php and add this 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
{
    // Send 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
{
    //Send 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

To add a route for the receivesms function in the SMSController class, open the routes/web.php file and add these lines after the sendSMS route:

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

Now SMSController is ready. Use this command to receive an inbound message.

php artisan serve

Your local development server will be started and you can test the application for inbound messages via the URL http://localhost:8000/receivesms/.

Ngrok setup

To serve XML documents, your local server must connect with Plivo API services. For that, we recommend using ngrok, which exposes local servers running behind NATs and firewalls to the public internet over secure tunnels. Using ngrok, you can set webhooks that can talk to the Plivo server.

ngrok block diagram

Install ngrok and run it on the command line, specifying the port that hosts the application on which you want to receive messages (8000 in this case):

./ngrok http 8000

This starts the ngrok server on your local server.

Sample ngrok CLI

You should be able to see your basic server application in action at https://<nrgok_URL>/inbound/.

Sample ngrok CLI

Now people can send messages to your Plivo number.

You can follow the same approach to serve other XML documents to manage call flows. Refer to our detailed XML reference to see all the XML elements available on the Messaging API platform.