Get Started

Twilio Client PHP Quickstart

Let's walk through making our first phone call from your browser to Twilio with Twilio Client.

Hello Monkey Client

Copy the following code into the hello-client-monkey.php file on your web server, substituting your own Account SID and Auth Token for the ones in the accountSid and authToken lines.

Let's say your server's domain is companyfoo.com. Assuming you put the PHP file in the document root, the URL will be: http://companyfoo.com/hello-client-monkey.php. Load that URL in your browser -- it will display a button that will initiate an audio connection into Twilio when clicked. The first few lines of code generates a secure Capability Token that Twilio Client uses to authorize the connection (using your account credentials).

We also registered two Twilio Client callbacks:

  1. ready - callback for when the client is successfully registered with Twilio.
  2. error - callback for when an error happens, such as a connection error.
  • quickstart/client/hello-client-monkey.php
    <?php
    include 'Services/Twilio/Capability.php';
    
    // put your Twilio API credentials here
    $accountSid = 'ACxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx';
    $authToken  = 'yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy';
    
    $capability = new Services_Twilio_Capability($accountSid, $authToken);
    $capability->allowClientOutgoing('APabe7650f654fc34655fc81ae71caa3ff');
    $token = $capability->generateToken();
    ?>
    
    <!DOCTYPE html>
    <html>
      <head>
        <title>Hello Client Monkey 1</title>
        <script type="text/javascript"
          src="//static.twilio.com/libs/twiliojs/1.1/twilio.min.js"></script>
        <script type="text/javascript"
          src="https://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js">
        </script>
        <link href="http://static0.twilio.com/packages/quickstart/client.css"
          type="text/css" rel="stylesheet" />
        <script type="text/javascript">
    
          Twilio.Device.setup("<?php echo $token; ?>");
    
          Twilio.Device.ready(function (device) {
            $("#log").text("Ready");
          });
    
          Twilio.Device.error(function (error) {
            $("#log").text("Error: " + error.message);
          });
    
          Twilio.Device.connect(function (conn) {
            $("#log").text("Successfully established call");
          });
    
          function call() {
            Twilio.Device.connect();
          }
        </script>
      </head>
      <body>
        <button class="call" onclick="call();">
          Call
        </button>
    
        <div id="log">Loading pigeons...</div>
      </body>
    </html>
        

You are now ready to make the call, so go ahead and click the 'Call' button! Did you hear the welcome message? Cool!

But hey, how do you close the connection and hangup the call? Keep reading...


Next: Hanging Up Calls »