Twilio Client PHP Quickstart

Receiving Connections

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 allowClientOutgoing with the Application Sid of your Twilio Sandbox. You can find this Application Sid on the Sandbox portion of your Account Dashboard.

    • quickstart/client/hello-client-monkey-3.php
      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.

      Now we need a Twilio phone number (for this tutorial, we'll just use your Twilio Sandbox number). Configure the Voice URL on your Sandbox (in the Account 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 Sandbox 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 »