In this recipe, we're going to set up a simple Twilio Client app.
We'll set up our app to let you make a call by pushing a button. For now, the call will just be a welcome message.
This first recipe will set up a basic Twilio Client app in the following manner:
Services/
folder to your website.config.php
to your web server using the following code:<?php $accountsid = ''; // YOUR TWILIO ACCOUNT SID $authtoken = ''; // YOUR TWILIO AUTH TOKEN $appsid = ''; // YOUR TWILIO APP SID ?>
hello.php
to your web server using the following code:<?php include("config.php"); include 'Services/Twilio/Capability.php'; $capability = new Services_Twilio_Capability($accountsid, $authtoken); $capability->allowClientOutgoing($appsid); $token = $capability->generateToken(); ?> <!DOCTYPE html> <html> <head> <title>Hello Client</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> <?php include("clientjs.php");?> </head> <body> <button class="call" onclick="call();">Call</button> <button class="hangup" onclick="hangup();">Hangup </button> <div id="log"></div> </body> </html>
This sets up our base client interface and gets the token we will use for our app.
clientjs.php
to your web server using the following code:<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"); }); Twilio.Device.disconnect(function (conn) { $("#log").text("Call ended"); }); function call() { Twilio.Device.connect(); } function hangup() { Twilio.Device.disconnectAll(); } </script>
The preceding code is our base Twilio Client code; it sets up instructions on what our app will do when it talks to Twilio.
In this case, we tell it to update div
with the ID "log"
when we are ready to make calls, when we make a call, and when the call is ended.
clientjs.php
file in your web server, and you will see a button to begin a call. Click on the button to begin the call, and you will be prompted to allow Twilio Client to access your microphone. Click on allow. When you make the call, you will hear a welcome message.In steps 1, 2, and 3, we set up a TwiML app in our Twilio account.
In steps 4 and 5, we downloaded and set up our Twilio PHP library.
In step 6, we set up our config.php
file with our Twilio settings.
In step 7, we uploaded hello.php
, which is our Twilio Client app.
In step 8, we uploaded our clientjs.php
file, which contains our Twilio Client JavaScript code. This code is used to handle the process of talking to Twilio from our web app.
This is a basic example that allows you to make a call and hang up. In the upcoming recipes, we'll have a look at receiving incoming calls and making outgoing calls.
The complete code for this recipe can be found in the Recipe1
folder under Code
.