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:
If you have any issues creating a Plivo account, please reach out to our Support Team for assistance.
You must set up and install PHP and Plivo’s PHP SDK to make your first call. Here’s how.
Operating System | Instructions |
---|---|
OS X | You can install PHP using the official installer. You can also install it from here. |
Linux | To install PHP on Linux you can find the instructions here. |
Windows | To install PHP on Windows you can use the official installer. |
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.
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
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.
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.
You can also check the version of Composer by running the following command:
$ composer --version.
Run the following command:
$ curl -sS https://getcomposer.org/installer | php
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
Run the following 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: Make sure to allow Windows Installer for Composer to make changes to your php.ini file.
Run the Composer command.
$ composer -V
Create a project directory, run the following command:
$ mkdir myphpapp
Change the directory to our project directory in the command line:
$ cd myphpapp
To install the stable release, run the following command in the project directory:
$ composer require plivo/plivo-php
To install a specific release, run the following command in the project directory:
$ composer require plivo/plivo-php:4.3.1
To test the features in the beta release, run the following command in the project directory:
$ composer require plivo/plivo-php:v4.2-beta1
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'
This section will guide you through how to use Plivo APIs to make voice calls 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!
Plivo Phone Number(Optional): You can purchase numbers from the Numbers section of your Plivo Console and use the same as the caller ID for the outbound call. This number will also help you receive incoming calls as you must have a voice-enabled Plivo phone number to do the same. Please note that you can also purchase numbers using the Numbers API.
Answer Url: When a call is answered by the destination_number, you can control the call flow with the help of the answer_url set in the API request. Plivo will invoke the answer_url specified as soon as the call is answered and expect a valid XML response with instructions to handle the call. For example, you can use https://s3.amazonaws.com/static.plivo.com/answer.xml as answer_url to test your first outgoing call. The XML response of this is:
<Response>
<Speak>Congratulations! You've made your first outbound call!</Speak>
</Response>
As you can tell, the XML above holds instructions to say ‘Congratulations! You’ve made your first outbound call!’ to the callee. You can find the entire list of Plivo XML verbs and their details in the XML Reference section of the website here.
Now, create a file called MakeCall.php and paste the following code:
1
2
3
4
5
6
7
8
9
10
11
12
13
<?php
require 'vendor/autoload.php';
use Plivo\RestClient;
$auth_id = "Your AUTH_ID";
$auth_token = "Your AUTH_TOKEN";
$p = new RestClient($auth_id, $auth_token);
$response = $client->calls->create('the_callerID',
['the_destination_number'],
'http://s3.amazonaws.com/static.plivo.com/answer.xml',
'GET');
print_r($response);
Save the file and use the following command to run your code.
php MakeCall.php
In addition to having at least one voice-enabled Plivo phone number that is needed to receive incoming calls (as was mentioned earlier), you need the following:
When a call is received on a Plivo voice-enabled number, you can control the call flow by declaring an answer URL for your Plivo application associated with that Plivo phone number. Plivo will invoke the answer URL specified and expect a valid XML response to handle the call.
Notice how the concept of Answer URLs applies to both outbound API calls as well as incoming calls to your Plivo numbers. In the outbound API call example above, we specified the answer URL along with the make call API request, whereas in the case of incoming calls to Plivo numbers, the answer URL is specified in the Plivo application associated with the phone number.
In addition to requests to the answer URL, Plivo initiates HTTP requests to your application server through the course of a call based on the specific XML elements in your answer XML. Such requests can be broadly classified into two categories:
Action URL requests: XML instructions to carry forward the call are expected in response to these requests. These requests are typically invoked at the end of an XML element’s execution. For example: when an IVR input is received from the caller during a GetInput XML execution.
Callback URL requests: No XML instructions are expected in response to these requests. Such requests serve as webhooks to notify your application server of important events through the course of an XML element’s execution. For example: when a conference participant is muted or unmuted.
To be able to host Answer and Callback URLs and to be able to provide valid XMLs and accept notifications on these URLs respectively, you need to host a webserver at your end.
In this section, we’ll walk you through how to set up a PHP server in under five minutes and start handling incoming calls & callbacks.
Use the following code snippet to start a local server.
1
2
3
4
5
6
7
8
9
10
11
<?php
require '../vendor/autoload.php';
use Plivo\XML\Response;
$response = new Response();
$speak_body = "Hello, you just received your first call";
$response->addSpeak($speak_body);
Header('Content-type: text/xml');
echo($response->toXML());
?>
Save this code in any file (name the file something like receive_call.php
). To run this file on the server, go to the folder where this file resides and use the following command:
php -S localhost:8000
You can choose to run it on any other IP or port that your machine has access to. You should see your basic server app in action on http://localhost:8000/receive_call.php
To receive incoming calls 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.
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 80, run the following command:
./ngrok http <port_on_which_your_local_server_is_running>
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.
New Application
. You can also use Plivo’s Application API.Receive_call
. Enter your server URL (e.g. http://example.com/receive_call.php
) in the Answer URL
field and set the method as POST
. See our Application API docs to learn how to modify your application through our APIs.Create
to save your application.Receive Call
(name of the app) from the Plivo App dropdown list.Update
to save.Make a call to your Plivo number using a regular mobile phone. Plivo will send a request to your Answer URL
requesting for a valid XML response and process the call. Meanwhile, the parameters listed in the XML Request - call status documentation will also be sent to your server.
To forward an incoming call, you need to use Dial XML.
Create a file called forward_call.php
and paste the below code.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
<?php
require '../vendor/autoload.php';
use Plivo\XML\Response;
$response = new Response();
$params = array(
'action' => "https://www.foo.com/dial_status/",
'method' => "POST",
'redirect' => "true"
);
$dial = $response->addDial($params);
$number = "14153336666"; // call wll be forwarded to this number
$dial->addNumber($number);
Header('Content-type: text/xml');
echo($response->toXML());
Make a call to your Plivo number using a regular mobile phone. Plivo will send a request to your Answer URL
requesting for a valid XML response and process the call. Once Plivo receives the dial XML response, the call will be forwarded to the number defined in the XML. Meanwhile, the parameters listed in the XML Request - call status documentation will also be sent to your server.