PHP Quickstart: Hanging Up Calls

Hanging Up Calls

Congrats, you made your first audio connection from your browser to Twilio! You should have heard a welcome message when you made the call.

However, as you probably noticed, there was no way to close the connection from your client. For now, you can simply refresh the page, but shouldn't there be a button for that? So, let’s add one to the page we created to let you close the audio connection.

The highlighted lines are what we're adding:

    include 'Services/Twilio/Capability.php';

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

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

    <!DOCTYPE html>
        <title>Hello Client Monkey 2</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");

          function call() {

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

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

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

And that’s it! Go ahead and make another call. You can now press the hangup button at any time and your connection will close.

Also, the above example added code to register two more callback handlers. Whenever something interesting happens within Twilio Client, the registered callbacks will be triggered. The callback registered to Twilio.Device.connect is triggered when the connection is successfully established, and the callback registered to Twilio.Device.disconnect is triggered when it is closed. In the above example, the callbacks simply change the log text, but in your application they could do anything.

Now that you can hang up calls, let's learn how to receive incoming calls in your browser!

Next: Receiving Incoming Calls »