Twilio Client makes it easy to add browser calling to your website using a usb headset or microphone. You can use Click-to-call to connect your web user with a traditional phone number such as a sales line with just the click of a button. This example shows browser based click-to-call using Twilio Client. You can also read our traditional phone click-to-call HowTo.
A user clicks a 'Start Call' button on a web page. Twilio will call a specified third party phone and setup a call between the user's browser and that party's phone number. A call status and a disconnect button are displayed on the web page during the call. The parties may talk until one of them disconnects.
This HowTo demonstrates browser calling using Twilio Client, specifying an outgoing connection to a Twilio Application, and creating TwiML call logic using the <Say> and <Dial> verbs.
Create a capability token to securely bind Twilio Client to your account and also give permissions to make outbound calls to a Twilio Application.
include 'Services/Twilio/Capability.php';
$accountSid = 'ACxxxxxxxxxxxxxxx';
$authToken = 'xxxxxxxxxxxxxxxxx';
$capability = new Services_Twilio_Capability($accountSid, $authToken);
$capability->allowClientOutgoing('APxxxxxxxxxxxxxxx');
<script type="text/javascript" src="//static.twilio.com/libs/twiliojs/1.1/twilio.min.js"></script>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.1/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function(){
Twilio.Device.setup("<?php echo $capability->generateToken();?>");
$("#call").click(function() {
Twilio.Device.connect();
});
$("#hangup").click(function() {
Twilio.Device.disconnectAll();
});
Twilio.Device.ready(function (device) {
$('#status').text('Ready to start call');
});
Twilio.Device.offline(function (device) {
$('#status').text('Offline');
});
Twilio.Device.error(function (error) {
$('#status').text(error);
});
Twilio.Device.connect(function (conn) {
$('#status').text("Successfully established call");
toggleCallStatus();
});
Twilio.Device.disconnect(function (conn) {
$('#status').text("Call ended");
toggleCallStatus();
});
function toggleCallStatus(){
$('#call').toggle();
$('#hangup').toggle();
}
});
</script>
<div align="center">
<input type="button" id="call" value="Start Call"/>
<input type="button" id="hangup" value="Disconnect Call" style="display:none;"/>
<div id="status">
Offline
</div>
</div>
<?xml version="1.0" encoding="UTF-8"?> <Response> <Say>Placing your call now.</Say> <Dial callerId="NNNNNNNNNN">4155551212</Dial> </Response>
Our TwiML speaks audio to the web user using <Say> verb, then initiates a phone call to the third party phone number via the <Dial> verb. The 'callerId' attribute is required and determines what number the third party sees on their phone when it rings. For a complete overview of TwiML (Twilio's XML) see the TwiML documentation.