twilio REST api打电话的问题

时间:2011-08-10 15:58:50

标签: php rest twilio

一个

现在我使用Dial动词完成顺序拨号但现在我希望我的应用程序执行拨号动词不起作用的呼叫,因此我需要REST api .....

我无法弄清楚如何做到这一点,我是REST新手。使用超时是否会跳过行?如果超时工作,那么也许我可以使这项工作,但除此之外我真的没有想法..

另外,如何在REST中获取呼叫状态?

让我们说我的代码看起来像这样,如何更改它以获取通话状态并在通话时设置超时?

<?php
// Include the Twilio PHP library
require 'Services/Twilio.php';
// Twilio REST API version
$version = "2010-04-01";
// Set our Account SID and AuthToken
$sid = 'AC123';
$token = 'abcd';
// A phone number you have previously validated with Twilio
$phonenumber = '4151234567';
// Instantiate a new Twilio Rest Client
$client = new Services_Twilio($sid, $token, $version);
try {
// Initiate a new outbound call
$call = $client->account->calls->create(
$phonenumber, // The number of the phone initiating the call
'5101234567', // The number of the phone receiving call
'http://demo.twilio.com/welcom/voice/'
);
echo 'Started call: ' . $call->sid;
} catch (Exception $e) {
echo 'Error: ' . $e->getMessage();
}

1 个答案:

答案 0 :(得分:4)

使用PHP helper library

<?php
// Include the Twilio PHP library
require 'Services/Twilio.php';
// Twilio REST API version
$version = "2010-04-01";
// Set our Account SID and AuthToken
$sid = 'AC123';
$token = 'abcd';
// A phone number you have previously validated with Twilio
$phonenumber = '4151234567';
// Instantiate a new Twilio Rest Client
$client = new Services_Twilio($sid, $token, $version);
try {
// Initiate a new outbound call
$call = $client->account->calls->create(
$phonenumber, // The number of the phone initiating the call
'5101234567', // The number of the phone receiving call
'http://demo.twilio.com/welcom/voice/',
array('timeout'=>'15','ifmachine'=>'hangup','status_callback'=>'yourNextNumberHandler.php')
);
echo 'Started call: ' . $call->sid;
echo 'The status of the call is '.$call->status;
} catch (Exception $e) {
echo 'Error: ' . $e->getMessage();
}
?>

此代码基于https://github.com/twilio/twilio-php/blame/master/docs/api/rest.rst

中的文档

所以我做了几件事:

  1. 为拨出电话添加了一系列参数,以便:

    • 设置超时(注意:如果不起作用,可能不是字符串'15' 尝试15作为数字)

    • 确定机器应答时要做什么(在这种情况下我选择了 挂断)

    • 确定呼叫结束时要做什么(在这种情况下,Twilio要求'yourNextNumberHandler.php'处理下一个号码)

  2. 在底部,我们有echo 'The status of the call is '.$call->status;,可以在下列其中一个QUEUED,RINGING,IN-PROGRESS, COMPLETED,FAILED,BUSY,or NO_ANSWER中为您提供一个输出 处理多个调用的另一种方式是进行检查,如

    $ I = 0; $ myPhoneList = array('14162351836','16472871987',18003984785'); if($ call-&gt; status =='COMPLETED'){    //拨打一个新号码$ myPhoneList [$ i ++]; }

  3. 而不是使用'yourNextNumberHandler.php'参数

    的回调status_callback

    我没有那么多使用twilio,但我希望这有帮助