How to Receive a Phone Call in PHP with Laravel and Plivo

Making an outbound phone call using the Plivo Voice platform is easy, but communication should be a two-way street. Customers should be able to call you back, and you should answer the calls and address their concerns. This guide shows you how to receive incoming calls on Plivo numbers and manage the call flow once a call reaches the Plivo voice platform. To see how to do this, we’ll build a Laravel application to receive an incoming call and greet the caller with a text-to-speech (TTS) message.


Before you get started, you’ll need:

  • A Plivo account — sign up for one for free if you don’t have one already.
  • A voice-enabled Plivo phone number if you want to receive incoming calls. To search for and buy a number, go to Phone Numbers > Buy Numbers on the Plivo console.
  • Laravel and Plivo PHP packages.
  • ngrok — a utility that exposes your local development server to the internet over secure tunnels.

Create a Laravel application to receive incoming calls and play a TTS message

First, you need to install Laravel if you haven’t installed it already. We suggest using Composer to install it. Add a new Laravel project with boilerplate code with the command composer create-project laravel/laravel receive_calls --prefer-dist. This will create a receive_calls directory with the necessary folders and files for development. Then change to the newly created “receive_calls” project directory and install the Plivo PHP package (composer require plivo/plivo-php).

Once you’ve installed Laravel and the Plivo PHP SDK, run php artisan make:controller ReceivecallController to create a Laravel controller to handle incoming calls on a Plivo number. To handle an incoming call, you need to return an XML document from the URL configured as the answer URL in the application assigned to the Plivo number. The PHP SDK can manage the XML document generation, and you can use the Speak XML element to play a text-to-speech message to the caller. Use this code:


namespace App\Http\Controllers;

require '../vendor/autoload.php';
use Plivo\RestClient;
use Plivo\XML\Response;
use Illuminate\Http\Request;

class ReceivecallController extends Controller
   // Speak XML to handle your first incoming call
   public function receiveCalls()
       $response = new Response();
       $speak_body = "Hello, you just received your first call";
       Header('Content-type: text/xml');
       echo $response->toXML();

Now, you need to add a route for the “receiveCalls” function in the ReceivecallController class. Open the routes/web.php file and add this line at the end of the file:

Route::match(['get', 'post'], '/receive_calls', 'App\Http\Controllers\ReceivecallController@receiveCalls');

Note: We need to add the route of the app to the “except” array to disable CSRF verification - app/Http/Middleware/VerifyCsrfToken.php`


namespace App\Http\Middleware;

use Illuminate\Foundation\Http\Middleware\VerifyCsrfToken as Middleware;

class VerifyCsrfToken extends Middleware
    * The URIs that should be excluded from CSRF verification.
    * @var array
   protected $except = [

Test the code locally

Now the ReceivecallController is ready to handle incoming calls to your Plivo number using Laravel and Plivo PHP SDK. To run the code on the Laravel server, use the command

$ php artisan serve

You should see your basic server application in action on

Expose the local server to the internet using ngrok

Once you see the application working locally, the next step is to connect the application to the internet to return the XML document to process the incoming call. For that, we recommend using ngrok, which exposes local servers behind NATs and firewalls to the public internet over secure tunnels.

Install it and run ngrok on the command line, specifying the port that hosts the application on which you want to receive calls (8000 in this case, as our local Laravel application is running there):

$ ./ngrok http 8000

Ngrok will display a forwarding link that you can use as a webhook to access your local server over the public network.

Ngrok CLI

Test the link by opening the ngrok URL( in a browser. We used HTTPie to check the XML response from the ngrok URL.

XML document with Speak XML element

Connect the Laravel application to a Plivo number

The final step is to configure the application as a Plivo voice application and assign it to a Plivo number on which you want to receive incoming calls.

Go to the Plivo console and navigate to Voice > Applications > XML, then click on the Add New Application button in the upper right.

Provide a friendly name for the application — we used “App-Incoming-call” — and configure the ngrok URL as the Answer URL. Select the HTTP verb as POST, then click Create Application.

Create Plivo App to handle incoming calls

Now go to Phone Numbers > Your Numbers and click on the number to which you want to assign the application. From the Plivo Application drop-down, choose the voice application you just created. Finally, click Update Number.

Assign the Plivo App to a Plivo Number

Test the application

Make a phone call to the Plivo number you selected. You should see that the Laravel application automatically greets the caller with the text-to-speech message configured in the app.

And that’s how simple it is to receive an incoming call on a Plivo number and handle it using XML documents using Plivo’s PHP SDK and a Laravel application. You can implement other use cases on the Plivo Voice platform, such as phone system IVR, call forwarding, and number masking, as your business requires.

Haven’t tried Plivo yet? Getting started is easy and only takes five minutes. Sign up today.

comments powered by Disqus

By submitting this form, you agree we may contact you in the manner described in our Privacy Policy.