Set Up a PHP Dev Environment for Voice Calls

This guide shows how to set up a development environment in five minutes to trigger API requests and start serving Plivo XML to control your call flow. It also shows how to test your code 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 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

Change to the quickstart directory and run this command to create a Laravel controller for outbound calls.

php artisan make:controller VoiceController

This command generates a controller named VoiceController in the app/http/controllers/ directory. Edit the app/http/controllers/voiceController.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
<?php
namespace App\Http\Controllers;
require '../../vendor/autoload.php';
use Plivo\RestClient;
use Illuminate\Http\Request;

class VoiceController extends Controller
{
    // To make your first outbound call
    public function makeCall()
    {
        $auth_id    = "<auth_id>";
        $auth_token = "<auth_token>";
        $client     = new RestClient($auth_id, $auth_token);
        $response = $client->calls->create('<Caller_ID>',
        ['<Destination_Number>'],
        'http://s3.amazonaws.com/static.plivo.com/answer.xml',);
        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.
  • <Destination_Number> can also be a SIP endpoint. If you’re calling a SIP endpoint, the <Destination_Number> placeholder should be a valid SIP URI. Example: sip:john1234@phone.plivo.com

Add a route

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

Route::match(['get', 'post'], '/makecall', 'VoiceController@makeCall');
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'], '/makecall', 'App\Http\Controllers\VoiceController@makeCall');
  • We added the route of the app to the “except” array to disable CSRF verification — app/Http/Middleware/VerifyCsrfToken.php
ul

Now VoiceController is ready. Use this command to initiate an outbound call.

php artisan serve

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

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 Voice API platform.

Serve an XML document and manage callbacks

When you receive a call on a Plivo voice-enabled number, you can control the call flow by declaring an Answer URL for the Plivo application associated with that phone number. Plivo will invoke the Answer URL specified and expect a valid XML response to handle the call.

Note: You can use Answer URLs with both outbound API calls and inbound calls. In the outbound API call example above, we specified the Answer URL along with the Make Call API request; for incoming calls, you’d specify the Answer URL in the Plivo application associated with the phone number.

In addition to requests to the Answer URL, Plivo initiates other HTTP requests to your application server based on specific XML elements in your Answer XML document. Such requests are broadly classified into two categories:

Action URL requests: These requests are typically invoked at the end of an XML element’s execution, and the server expects XML instructions to carry forward the call in response to these requests. This happens, for example, when a caller provides Touch-Tone input during GetInput XML execution.

Callback URL requests: These requests serve as webhooks to pass the application server information about events through the course of an XML element’s execution, such as when a conference participant is muted or unmuted. No XML instructions are expected in response to these requests.

Set up a Laravel server to serve XML and manage callbacks

Here’s how to set up a Laravel server to serve XML documents and manage callbacks.

Edit the file app/http/controllers/VoiceController.php and add this code in the VoiceController class after the makeCall 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
<?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 call
    public function makeCall()
    {
        .......
    }

    // Speak XML to handle your first incoming call
    public function receiveCall()
    {
        $response = new Response();
        $speak_body = "Hello, you just received your first call";
        $response->addSpeak($speak_body);
        Header('Content-type: text/xml');
        echo $response->toXML();
    }
}

Add a route

To add a route for the receiveCall function in the VoiceController class, edit the file routes/web.php and add these lines after the makecall route:

Route::match(['get', 'post'], '/makecall', 'VoiceController@makeCall');
Route::match(['get', 'post'], '/receivecall', 'VoiceController@receiveCall');

Now VoiceController is ready. Use this command to receive an inbound call.

$php artisan serve

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

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 will start 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>/receive_call/ and check the XML response:

sample XML

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 Voice API platform.