Get Started with Connect Call to Second Person Using PHP

    Overview

    You may want to request your application to dial out for you, such that you’ll receive a call on your phone, which will then connect you to the user you want. With the help of this tutorial named connect a call to the second person, you may accomplish this. Step-by-step instructions are as follows:

    1. Make an outbound call to the caller.
    2. When the first user answers the phone, a new call is placed to a different user (second user).
    3. Both calls (first and second user) are bridged once the second user answers the call.

    Common Connect call to second use cases include:

    • Call Forward: You can forward calls to another user whom you wish for.
    • Click to Call: You can direct calls to the right representative as well as the person who clicked on the link.
    • Lead assignment and lead routing: For inbound sales calls, you can set up an IVR menu with a set of qualifying questions to discover a customer’s interests, then redirect their call to a representative based on their responses.

    This guide shows how to build Connect a user to second person on the Plivo platform, 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 connect a call to second user 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.

    Implementation

    In this section, we will guide you to create a PHLO to implement connect a call to second use-case.

    Prerequisites

    1. 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! Find Your Auth Credentials on Plivo Console
    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. Buy a New Plivo Number

    Create the PHLO

    Create a PHLO for Connect a Call to Second User

    1. 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.
    2. Click CREATE NEW PHLO to build a new PHLO.
    3. In 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.
    4. Click the Start node to open the Configurations tab, and then enter the information to retrieve from the HTTP Request payload. Only the keys from, to, and second_user will be filled in here.
    5. Click Validate to save the configurations for the node.
    6. 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.
    7. Connect the Start node with the Initiate Call node, using the API Request trigger state.
    8. Configure the Initiate Call node to select the From and To numbers from 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.
    9. Click Validate to save the configurations for the node.
    10. From the list of components on the left-hand side, drag and drop the Play Audio component onto the canvas. This will add a Play Audio node onto the canvas.
    11. Connect the Initiate Call node with the Play Audio node, using the Answered trigger state.
    12. Configure the Play Audio node with the text of your choice, For example: Please wait while we connect your call to the second number.
    13. Click Validate to to save the configurations of the node.
    14. From the list of components on the left-hand side, drag and drop the Call Forward component onto the canvas. This will add a Call Forward node onto the canvas.
    15. Connect the Call Forward node with the Play Audio node, using the Prompt Completed trigger state.
    16. Configure the Call Forward node to select the From and To numbers from 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
    17. Click Validate to to save the configurations of the node.
    18. 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:

    Connect Second User

    • After you complete the configurations, provide a friendly name for your PHLO and click Save. Your PHLO is now ready. You can trigger the PHLO and test it out. For more information, refer to the below section.

    Set up Your PHP Dev Environment

    You must set up and install PHP and Plivo’s PHP SDK. 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 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, follow the below steps.

    • Select the Start node - under API request, fill the Payload key as from, to & second_user and keep the values empty, then click validate & save.

    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", "second_user" => "+14157778887"]); // 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.

    To implement connect a call to second person use-case in the traditional XML way, you can refer to the instructions in the below section to begin your implementation.

    Implementation

    In this section, we will guide you in setting up an app to make a call to a number and when answered, connect the call to second person it using Plivo XML. In this example, when an outgoing call is answered, the call will be made to the second person using the <Dial> XML.

    Prequisites

    1. 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!

      Find Your Auth Credentials on Plivo Console

    2. Plivo Phone Number(Optional): 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.

      Buy a New Plivo Number

    3. Answer URL: When an outgoing call is made number, you can control the call flow by declaring an answer URL. Plivo will invoke the answer URL specified and expect a valid XML response to handle the call. In the outbound API call example in the Make-Outbound-Calls use-case guide, 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.

    4. Set Up Your Web Server: To be able to host Answer URL and to be able to provide valid XMLs and accept notifications on these URLs respectively, you need to host a webserver at your end. To set up your Web Server in your preferred programming language, please refer to the instructions available in the Set Up Laravel server section.

    Set Up Laravel server

    In this section, we’ll walk you through how to set up a Laravel server in under five minutes and start handling calls.

    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 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 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 Laravel & Create a Laravel Project

    • Use the below command to install Laravel:

      $ composer require laravel/installer
      

    As we have Laravel and its dependencies installed, we can use them to create a new Laravel project. As the initial step, using Laravel we can auto-generate code in the Laravel folder structure.

    • Change the directory to our project directory in the command line:

      $ cd mylaravelapp
      
    • Use the below command to start your Laravel project:

      $ composer create-project laravel/laravel quickstart --prefer-dist
      
    • To install the stable release, run the following command in the project directory:

      $ composer require plivo/plivo-php
      

    This will create a quickstart directory with the necessary folders & files for development.

    Install Plivo

    • To install a specific release, run the following command in the project directory:

      $ composer require plivo/plivo-php:4.15.0
      
    • 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'
    

    Create a Laravel Controller to Connect Calls to second person

    Change the directory to our newly created project directory, i.e, quickstart directory and run the below command to create a Laravel controller for inbound calls.

    $ php artisan make:controller VoiceController
    

    This will generate a controller named VoiceController in the app/http/controllers/ directory. Now, You have to open the app/http/controllers/voiceController.php file and add the following code:

    Now, open the app/http/controllers/VoiceController.php file and add include the following code in the VoiceController:

    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
    27
    28
    29
    30
    
    <?php
    namespace App\Http\Controllers;
    require '../../vendor/autoload.php';
    use Plivo\XML\Response;
    use Plivo\RestClient;
    
    class VoiceController extends Controller
    {
        public function outboundCall()
        {
            $host = request()->getHttpHost();
            $client = new RestClient('<auth_id>','<auth_token>');
            $response = $client->calls->create(
                '+14151234567',
                ['+14157654321'],
                'https://'.$host.'/connect',
                'POST');
            echo json_encode($response);
        }
    
        public function connect()
        {       
                $response = new Response();
                $response->addSpeak('Please wait while we connect your call');
                $dial = $response->addDial();
                $dial->addNumber('+14151234567'); // Dial to second number
                Header('Content-type: text/xml');
                echo ($response->toXML());
        }
    }
    
    Note:
    • Replace the placeholders auth_id & auth_token with your credentials from yourPlivo 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
    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.

    Add a Route

    Now, you need to add a route for the forward function in VoiceController class, open the routes/web.php file and add the below line:

    Route::match(['get','post'], '/outboundCall', 'App\Http\Controllers\VoiceController@outboundCall');
    Route::match(['get','post'], '/connect', 'App\Http\Controllers\VoiceController@connect');
    

    Now the VoiceController is ready to make calls, you can use the below command to start the Laravel server to make calls and to handle callbacks.

    $ php artisan serve
    

    Your local development server will be started and you can test the rails app for outbound calls via the URL http://127.0.0.1:8000/outboundCall/.

    Exposing your local server to the internet

    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.

    ngrok block diagram

    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 8000, run the following command:

    $ ./ngrok http 8000
    

    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.

    Sample ngrok CLI

    You can check the app in action on https://ff3e00858d94.ngrok.io/outboundCall.

    Test and validate

    Make a call to a regular mobile phone. Plivo will send a request to your Answer URL requesting for a valid XML response and connect the call to second user. Meanwhile, the parameters listed in the XML Request - call status documentation will also be sent to your server.