Showing posts with label TWILIO. Show all posts
Showing posts with label TWILIO. Show all posts

Friday, March 18, 2016

Twilio Voice call integration using PHP and JS

First step is to signup on twilio using a valid mobile number - which will get a verification code which is used to verify the mobile number.


On clicking the Get Your First Twilio Phone Number, a sample phone number will be provided by twilio which will be used to generate calls (From number).


Download the sdk and setup the code to autoload the sdk in the project.
Go to Dashboard click on ‘view API credentials’ to get the id and token.

Now go to and create a sample TwiML app. On the voice section provide an accessible url from which twilio will be fetching the from and to numbers for connecting (will explain in detail below).
E.g. http://DOMAIN/twilioCall.php

Generate required files for initiating calls.

demoCall.php : Display the dial-er page.

include 'Services/Twilio/Capability.php';
// put your Twilio API credentials here
$accountSid = 'AccountId';
$authToken  = 'token';
// put your Twilio Application Sid here
// and select an app to get the app id
$appSid     = 'TwiMLAppID';
$capability = new Services_Twilio_Capability($accountSid, $authToken);
$token = $capability->generateToken();

<!DOCTYPE html>
    <title>Call Demo</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() {
        // get the phone number to connect the call to
        params = {"PhoneNumber": $("#number").val()};

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

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

    <input type="text" id="number" name="number" placeholder="Enter a phone number to call"/>
    <div id="log">Loading ...</div>


header('Content-type: text/xml');
if (isset($_REQUEST['PhoneNumber'])) {
    $number = htmlspecialchars($_REQUEST['PhoneNumber']);

    <Dial callerId="<?php echo $callerId ?>">
 <Number>"<?php echo $number ?>"</Number>

Key elements required for connection are Account SId, Token ( and appId ( and select an app to get the app id).

Now its ready to be tested. Note that since this is a demo account from and to numbers cannot be changed (From should be the number provided by Twilio during signup and to should be the verified number on the account).

Logs will be available over here : , on clicking the timestamp it will direct to the details of request and response., this one provide alerts created during our test failures.


Alert logs