Set up your PHP dev environment for Voice Calls

    In this guide, you will learn how to set up a development environment in under 5 minutes to trigger API requests and to start serving valid Plivo XML to control your call flow. Also, Using a tunnelling software like ngrok, we will see how you can expose your local dev server with a public address to test out your integration with Plivo.


    Install PHP and the Plivo PHP SDK

    You must set up and install PHP and Plivo’s PHP SDK to make your first call. Here’s how.

    Install PHP

    Operating SystemInstructions
    OS XYou 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 | 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 Plivo PHP Package

    • 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
    • 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.

    require 'vendor/autoload.php'

    Trigger an API Request

    To trigger any Plivo API, you can create a file in the project directory and execute the code. Please check below example to make an outbound call:

    • Create a file called MakeCall.php and paste the below code.
    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('+14151234567',
    • Replace the placeholders of mandatory parameters to make outbound call using Plivo API.
    • Replace the placeholders auth_id & auth_token with your credentials from Plivo 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 making calls to.
    • Both +14151234567 and +14157654321 should be in E.164 format
    • Save the file and use the below command to run it.
    php MakeCall.php
    Note: You can follow the same approach to trigger any other API requests. Please refer to the detailed API reference to check all the API requests available on the Voice API platform.

    Serve an XML Document and Manage Callbacks

    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. We will see how to create and set up a new Plivo Application through the Plivo console in the following section.

    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.

    Set up a PHP Server to Serve XML & Manage Callbacks

    In this section, we’ll walk you through how to set up a PHP server in under five minutes to serve XML documents & manage callbacks.

    Use the following code snippet to start a local server.

        require '../vendor/autoload.php';
        use Plivo\XML\Response;
        $response = new Response();
        $speak_body = "Hello, you just received your first call";
        Header('Content-type: text/xml');

    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

    And you should see your basic server app in action on http://localhost:8000/receive_call.php

    Ngrok Setup

    To server XML responses and to handle callbacks, your local server should be able to connect with 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 which can talk to Plivo server.

    ngrok block diagram

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

    Use Ngrok to Expose Your Local Server to the Internet

    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 start the ngrok server on your local server. Refer to the below screenshot.

    Sample ngrok CLI

    This will give you a UI with links that look like* which you can use to access your local server using the public network. And you should see your basic server app in action on http://<nrgok_URL>/receive_call.php/

    Sample ngrok CLI

    Note: You can follow the same approach to serve any other XML documents to manage various call flows. Please refer to the detailed XML reference to check all the XML elements available on the Voice API platform.