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.
To get started, install PHP, the Composer dependency manager, the Laravel web framework, and Plivo’s PHP SDK.
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. |
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.
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.
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
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
You can also check the version of Composer by running this command:
$ composer --version.
Run the command
$ curl -sS https://getcomposer.org/installer | php
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.
Run this command to make Composer globally available for all system users:
$ mv composer.phar /usr/local/bin/composer
Download and run the Windows Installer for Composer.
Note: Allow Windows Installer for Composer to make changes to your php.ini file.
Run the Composer command.
$ composer -V
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.
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'
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);
}
}
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');
Route::match(['get', 'post'], '/makecall', 'App\Http\Controllers\VoiceController@makeCall');
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.
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.
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.
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();
}
}
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/.
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.
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.
You should be able to see your basic server application in action at https://<nrgok_URL>/receive_call/ and check the XML response:
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.