Create Your Own Conference Line in PHP using Twilio Programmable Voice

March 24, 2020
Written by

Create Your Own Conference Line in PHP using Twilio Programmable Voice

As much of the world is grappling with the overnight shift to work from home, the trusted conference call is a staple that is needed now more than ever. Teams that have only been separated by floors and walls are now teleworking from makeshift offices in kitchens and bedrooms.

While many software-as-a-service solutions exist, your company or organization may desire to roll out your own custom conference call solution or add it to an existing application. If PHP is your language of choice, this quick tutorial will teach you how to build your own conference line in PHP using Twilio Programmable Voice. Your team members will actually be able to call it directly and speak with one another from anywhere in the world.

If Node.js happens to be your preferred language, Sam Agnew just wrote an awesome version of this tutorial on the Twilio blog.

Prerequisites

This tutorial is a build-it-yourself solution, so you will need your own Twilio phone number to connect your team members. If you don’t have one or need an account, take a moment and sign up using the following link.

You will need the following dependencies to create this PHP conference line:

Please make sure that you have them installed and configured before you begin.

Create a New Project Directory

For demonstration purposes, the application will be developed locally on your computer. Ideally, you would want to have this application hosted in the cloud for continuous connections and use, but for now, we’ll assume that all work will exist locally.

Select the most ideal place to keep projects on your computer and run the following command in your terminal:

$ mkdir twilio-conference && cd twilio-conference

The above commands simply created a project folder and set it as the current folder for us to work from.

Create a New Composer.json File to Install the Twilio SDK

Composer is a popular packager manager for PHP and we will follow that trend, using it to include the Twilio PHP SDK in the subsequent steps. Create the required composer.json file in our project folder using the following command.

When prompted, confirm the Author, press enter for the remaining options, when presented with “Would you like to define your dev dependencies (require-dev) interactively?”, input n, and press enter when asked “Would you like to install dependencies now?”

$ composer init --require=twilio/sdk

The output will look similar to that below:

This command will guide you through creating your composer.json config.

Package name (<vendor>/<name>) [mbattle/twilio-conference]: Description []: 
Author [Marcus Battle <themarcusbattle@gmail.com>, n to skip]: Marcus Battle <mbattle@twilio.com>             
Minimum Stability []: 
Package Type (e.g. library, project, metapackage, composer-plugin) []: 
License []: 

Define your dependencies.

Using version ^6.1 for twilio/sdk
Would you like to define your dev dependencies (require-dev) interactively [yes]? n

{
    "name": "mbattle/twilio-conference",
    "require": {
        "twilio/sdk": "^6.1"
    },
    "authors": [
        {
            "name": "Marcus Battle",
            "email": "mbattle@twilio.com"
        }
    ]
}

Do you confirm generation [yes]? 
Would you like to install dependencies now [yes]? 
Loading composer repositories with package information
Updating dependencies (including require-dev)
Package operations: 1 install, 0 updates, 0 removals
  - Installing twilio/sdk (6.1.0): Loading from cache
twilio/sdk suggests installing guzzlehttp/guzzle (An HTTP client to execute the API requests)
Writing lock file
Generating autoload files

The command created a new composer.json file with the Twilio PHP SDK defined as a dependency and install it. The Twilio PHP SDK will not only give us access to the Twilio Voice API, it will also make it easy for us to write the code necessary to create the conference line.

NOTE: If you are unfamiliar with Composer, there are a lot of great articles such as this one to further your education on the subject.

Create an Application to Handle Calls via PHP

Believe it or not, we’re almost done with the application. All that’s left is the actual code that processes the incoming call via a webhook in the Twilio console. Create that file now in your terminal.

$ touch conference.php

In your favorite IDE, add the following code to conference.php:

<?php
require_once './vendor/autoload.php';

use Twilio\TwiML\VoiceResponse;

$response = new VoiceResponse();
$dial = $response->dial('');
$dial->conference('Hotline');

echo $response;

This code is all we need! Isn’t it amazing what can be accomplished with just ten lines of PHP?

The code above imports and autoloads the Twilio SDK that we defined in the previous section. After it loads, we declare a new instance of the VoiceResponse object from the SDK. This object gives us access to the TwiML verbs needed to define a conference line dial and conference.

If you would like to explore more TwiML verbs, this page is a great resource.

Make Your Application Accessible to the Internet

At this point, the internet has no idea that the conference.php file even exists. In order to use it and connect to your phone number, you’ll need to make it accessible from your local computer. To do that we’ll first start your local webserver using PHP’s builtin server.

$ php -S localhost:8000

Now that your server is running, create a tunnel to expose it to the internet using ngrok.

$ ./ngrok http 8000

When ngrok loads, your terminal will start a new session similar to the screen below. Take note of the Forwarding URL as you will use this to create your webhook URL.

ngrok session

Connect the Twilio Webhook to Your Application

Navigate to your Twilio console and select the phone number that will work for your conference line. Under Voice & Fax there’s a section labeled “A call comes in”. In the field, input your Forwarding URL + /conference.php. In my instance, that complete URL is https://6f00701b.ngrok.io/conference.php.

Twilio Phone Number dashboard

Testing Your Conference Line

You’re done! Your conference call is now complete. You will need two phones to actually test the application.

Simply call your Twilio phone number on two separate lines and start talking!

Conclusion

This tutorial has shown us how simple it is to:

  • Create a new PHP project
  • Install the Twilio PHP SDK
  • Create a webhook
  • Connect the webhook to a Twilio phone number

You could extend this application even further by modifying the experience of the call such as signaling a beep when a user joins the call. I look forward to seeing what you build!

Marcus Battle is Twilio’s PHP Developer of Technical content. If you have any questions, use the following channels to reach out:

Email: mbattle@twilio.com
Twitter: @themarcusbattle