PHP Quickstart: Receive Incoming Calls in the Browser

Now that you know how to disconnect connections from the browser, it's time for your browser to start receiving incoming connections! By the end of this tutorial, you will be able to call into your browser from your phone.

In order to receive incoming connections, we'll do the following things:

  1. Give the browser a client name. The browser will use this name when it registers itself as a Twilio client.
  2. Add callbacks that notify your app of incoming connections.
  3. Write some TwiML that directs incoming calls to <Dial> your named <Client>.
The client identifier currently may only contain alpha-numeric and underscore characters.

The new additions to our code are highlighted below. This is the summary of the important changes:

  • The function allowClientIncoming('jenny') grants the browser permission to register itself with Twilio under the name 'jenny'. If your TwiML <Dial>s 'jenny', your browser will receive a connection request.
  • The callback registered to Twilio.Device.incoming is triggered when an incoming connection request is received.

You'll also need to update the initial GET method with a Twilio Client Application Sid using the allow_client_outgoing parameter. To get one, create a TwiML App on your Apps page.

include 'Services/Twilio/Capability.php';

// put your Twilio API credentials here
$accountSid = 'ACxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx';
$authToken  = 'yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy';

// put your Twilio Application Sid here
$appSid     = 'APzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz';

$capability = new Services_Twilio_Capability($accountSid, $authToken);
$token = $capability->generateToken();

    <!DOCTYPE html>
        <title>Hello Client Monkey 3</title>
        <script type="text/javascript"
        <script type="text/javascript"
        <link href="//"
          type="text/css" rel="stylesheet" />
        <script type="text/javascript">

          Twilio.Device.setup("<?php echo $token; ?>");

          Twilio.Device.ready(function (device) {

          Twilio.Device.error(function (error) {
            $("#log").text("Error: " + error.message);

          Twilio.Device.connect(function (conn) {
            $("#log").text("Successfully established call");

          Twilio.Device.disconnect(function (conn) {
            $("#log").text("Call ended");

          Twilio.Device.incoming(function (conn) {
            $("#log").text("Incoming connection from " + conn.parameters.From);
            // accept the incoming connection and start two-way audio

          function call() {

          function hangup() {
        <button class="call" onclick="call();">

        <button class="hangup" onclick="hangup();">

        <div id="log">Loading pigeons...</div>

Copy the following code into a file named hello-client-twiml.php on your web server. Assuming you put the file in the document root of your web site, the URL will be: When you're done, load that URL in your browser to make sure the script renders the TwiML snippet correctly.

header('Content-type: text/xml');

You need to add that URL to your App. Change the Voice URL on your Application (found on the Apps page of your Dashboard) to point to the TwiML file at

Reload your Twilio Client web page, and your browser is now ready to receive incoming calls! Call your Twilio Number from your phone and you should be connected to your browser, which will automatically answer the call... Now start talkin’!

Next: Making Outgoing Calls »