Get Started with Voice Notifications Using PHP

    Overview

    This guide shows how to send audio notifications using voice calls. You can play recorded audio when the call recipient answers or use text-to-speech, as we show here, combining static text with dynamic information that Plivo gets from a variable.

    You can use voice notification for use cases such as:

    • Order notification
    • Booking status
    • Delivery status
    • Flight cancellation/rescheduling
    • Two-factor authentication/one-time password
    • New offer notification
    • Account balance notification

    Implement voice notification either by using our PHLO visual workflow builder or our APIs and XML documents. Follow the instructions in one of the tabs below.

    To implement voice notifications use-case, you can create and deploy a PHLO with a few clicks on the PHLO canvas. PHLO also lets you visually construct your entire use-case. With PHLO, you only pay for calls you make/receive, and building with PHLO is free.

    Outline

    Implementation

    In this section, we will guide you to create a PHLO to implement voice notifications use-case.

    Prerequisites

    1. Create a Plivo Account(if you don’t have one already): You can sign up with your work email address and complete the phone verification step using your mobile number.
    2. Set up your Dotnet dev environment: To set up your dev environment in your .NET(C#) programming language, please refer to the instructions available in the Set up Your Dotnet Dev Environment section.
    3. Buy a Plivo number(optional): You must have a voice-enabled Plivo phone number if you are willing to receive incoming calls. You can purchase numbers from the Numbers section of your Plivo Console. It is also possible to purchase numbers using the Numbers API.

    Create the PHLO

    • On the top navigation bar, click PHLO. The PHLO page will appear and display your existing PHLOs, if any exist. If this is your first PHLO, then the PHLO page will be empty.

    • Click CREATE NEW PHLO to build a new PHLO.

    • On the Choose your use-case window, click Build my own. The PHLO canvas will appear with the Start node. Note: The Start node is the starting point of any PHLO. You can choose between the four available trigger states of the Start node; Incoming SMS, Incoming Call, and API Request. For this PHLO, we will use the API Request trigger state.
    • Click the Start node to open the Configurations tab.
    • Then, enter the information to retrieve from the HTTP Request payload. For example, this could be a variable for the list of numbers to call.

    • From the list of components on the left-hand side, drag and drop the Initiate Call component onto the canvas. This will add a Initiate call node onto the canvas.

    • Connect the Start node with the Initiate Call node, using the API Request trigger state.

    • The values for the From and To numbers are picked from the HTTP Request Payload. Make sure to clearly define the variables in the Start node.

    • Next, to configure the From and To numbers in the Initiate Call node, use the variables set in the Start node. Note:

      • Enter two curly brackets to view all available variables.
      • The values for the From and To numbers are picked from the HTTP Request Payload. Make sure to clearly define the variables in the Start node.

    • Similarly, create a node for the Play Audio component and connect it to the Initiate Call node using the Answered trigger state.

    • Next, configure the Play Audio node to play a specific message to the user.

      Note:

      • The audio playback can either be static or dynamic. In this example, we will use a combination of a static message along with some dynamic message.
      • On the Play Audio Configurations tab, enter a static message (For example, Your Flight status is:) in the Speak Text field, followed by a variable to include the dynamic text.
    Note:
    • You can choose a variable by entering two curly brackets in the Speak Text field.
    • Make sure you validate the configuration for each node.
    • After you complete the configurations, click Save. You can trigger your PHLO using the PHLO URL to test it out. Your complete PHLO will look like this:

      Voice Notification

    Set up Your PHP Dev Environment

    You must set up and install PHP and Plivo’s PHP SDK to implement voice notifications use-case. Here’s how.

    Install PHP

    Operating SystemInstructions
    macOSYou 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 instll 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 https://getcomposer.org/installer | 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
      
    • Alternatively, you can download this source and run

      $ composer install
      

    Trigger the PHLO

    Once you have created and setup your PHP dev envrironment, you can go to your Plivo Consolse and copy the PHLO_ID. You can integrate a PHLO into your application workflow by making an API request to trigger the PHLO with the required payload.

    With Static Payload

    You can choose to either configure the mandatory params required for a PHLO while creating the PHLO itself or, you can pass the params as payload while triggering the PHLO from your app.

    Note: When you configure the params while creating the PHLO, the values are hardcoded, and they act as static payload.

    With Static Payload

    Code

    Now, create a file called TriggerPhlo.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\Resources\PHLO\PhloRestClient;
    use Plivo\Exceptions\PlivoRestException;
    $client = new PhloRestClient("YOUR_AUTH_ID", "YOUR_AUTH_TOKEN");
    
    $phlo = $client->phlo->get("YOUR_PHLO_ID");
    try {
        $response = $phlo->run();
        print_r($response);
    } catch (PlivoRestException $ex) {
        print_r($ex);
    }
    

    With Dynamic Payload

    To use dynamic values for the parameters, you can use the liquid templating params while creating the PHLO and pass the values while triggering the PHLO.

    With Dynamic Payload

    Code

    Now, create a file called TriggerPhlo.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\Resources\PHLO\PhloRestClient;
    use Plivo\Exceptions\PlivoRestException;
    $client = new PhloRestClient("YOUR_AUTH_ID", "YOUR_AUTH_TOKEN");
    
    $phlo = $client->phlo->get("YOUR_PHLO_ID");
    try {
        $response = $phlo->run(["from" => "+14157778888", "to" => "+14157778889"]); // These are the fields entered in the PHLO console
        print_r($response);
    } catch (PlivoRestException $ex) {
        print_r($ex);
    }
    

    You can get your Auth_ID and Auth_token from your dashboard AUTHID

    You can find the PHLO_ID on the PHLO Listing page. PHLO Listing

    Test and Validate

    You can save the file and use the below command to run your code.

    $ php TriggerPhlo.php
    
    Note: If you are using a Plivo Trial account for this example, you can only make calls to phone numbers that have been verified with Plivo. Phone numbers can be verified at the Sandbox Numbers page.

    Here’s how to use Plivo APIs and XML to implement voice notifications.

    How it works

    Outbound Call Flow

    Plivo requests an answer URL when the call is answered (step 4) and expects the file at that address to hold a valid XML response from the application with instructions on how to handle the call. To see how this works, you can use https://s3.amazonaws.com/static.plivo.com/notification.xml as an answer URL to test your first outgoing call. The file contains this XML code:

    <Response> 
    <Speak>Congratulations, your order was successfully placed</Speak> 
    </Response>
    

    This code instructs Plivo to say, “Congratulations, your order was successfully placed” to the call recipient. You can find the entire list of valid Plivo XML verbs in our XML Reference documentation.

    Prerequisites

    To get started, you need a Plivo account — sign up with your work email address if you don’t have one already. If this is your first time using Plivo APIs, follow our instructions to set up a PHP development environment.

    Create a voice notification application in PHP

    Create a file called MakeCall.php and paste into it this code:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    
    <?php
    require 'vendor/autoload.php';
    use Plivo\RestClient;
    
    $auth_id    = "<auth_id>";
    $auth_token = "<auth_token>";
    $p          = new RestClient($auth_id, $auth_token);
    
    $response = $client->calls->create('<caller_id>',
                                     ['<destination_number>'],
                                     'https://s3.amazonaws.com/static.plivo.com/notification.xml',
                                     'GET');
    print_r($response);
    

    Replace the auth placeholders with your authentication credentials from the Plivo console. Replace the phone number placeholders with actual phone numbers in E.164 format (for example, +12025551234).

    Note: 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 Plivo will automatically fetch the values from the environment variables. You can use the $_ENV or putenv/getenv functions to store environment variables and fetch them when initializing the client.

    Test

    Save the file and run it.

    $ php MakeCall.php
    
    Note: If you’re using a Plivo Trial account, you can make calls only to phone numbers that have been verified with Plivo. You can verify (sandbox) a number by going to the console’s Phone Numbers > Sandbox Numbers page.