Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to know twilio call status( completed or not )

I am new in twilio api. In a web application am working on, i have to check call completed or not and I am sending wav file to twiml. If completed i have to deduct credit of user.. i am using the following code...

callMeAction

        $AccountSid = "**********************";
        $AuthToken = "***************";

        /* Your Twilio Number or an Outgoing Caller ID you have previously validated
          with Twilio */
        $from = '**************';

        /* Number you wish to call */
        $to = $_POST['contactno'];

        /* Directory location for callback.php file (for use in REST URL) */
        $url = 'http://'.$_SERVER['HTTP_HOST'].'/public/';

        /* Instantiate a new Twilio Rest Client */
        $client = new Services_Twilio($AccountSid, $AuthToken);



        /* make Twilio REST request to initiate outgoing call */
        $call = $client->account->calls->create($from, $to, $url . 'callback.php?number=' . $_POST['contactno'] . '&wav=' . $_POST['wav']);

        /* redirect back to the main page with CallSid */
        $msg = urlencode("Connecting... " . $call->sid);
        //header("Location: index.php?msg=$msg");

         $this->view->msg = $msg;

        if($call->status == 'COMPLETED'){

            /*
             *  Deduct credit if call completed
             */


          $this->view->msg = $msg;
        }

callback.php

<?php
    echo "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n";
?>



<?php if($_REQUEST['wav']){ ?>
<Response>
    <Say>A customer at the number <?php echo $_REQUEST['number']?> is calling</Say>
    <Dial><?php echo $_REQUEST['number']?></Dial>
    <Play><?php echo $_REQUEST['wav'] ;?></Play>
</Response>
<?php } ?>

please help me... Thanks In advance. :)

like image 710
Manoj Avatar asked Dec 06 '11 08:12

Manoj


1 Answers

Set a URL for the StatusCallback (docs halfway down this page), and put your charge logic in that script.

You pass the StatusCallback URL when creating the call, you should be able to pass an array of optional parameters as the 4th argument to: $client->account->calls->create().

For incoming calls, the URL is (optionally) defined for each number (or application, if you use that method).

like image 179
Tim Lytle Avatar answered Sep 28 '22 15:09

Tim Lytle