我正在关注https://www.twilio.com/docs/api/rest/change-call-state#post上的教程我在php中编写了允许您将当前入站呼叫转发到新Twiml URL的部分。我发现为了使其工作,我必须在更新数组中指定To和From参数。我需要将调用转发到指定的URL而不是To参数中指定的数字。但是,Twilio API会抛出一个错误,指出To参数是必需的,但文档表明它不是。我在这里做错了吗?
<?php
// Get the PHP helper library from twilio.com/docs/php/install
require_once('TwilioAPI/twilio-php-master/Services/Twilio.php'); // Loads the library
// Your Account Sid and Auth Token from twilio.com/user/account
$sid = 'XXXXXXX';
$token = 'XXXXXXX';
$callSid = $_POST['CallSid'];
$client = new Services_Twilio($sid, $token);
// Get an object from its sid. If you do not have a sid,
// check out the list resource examples on this page
$call = $client->account->calls->get($callSid);
$call->update(array(
"Url" => "http://ftstoo.com/Phone/TheFinalTouchSecurity/forwardToBob.xml",
"Method" => "POST"
));?>
forwardToBob.xml包含带有Say动词的Response。 这个PHP代码(不是twiml)抛出错误
未捕获的异常&#39; Services_Twilio_RestException&#39;有消息&#39;否&#39; To&#39;号码被指定&#39;在/home/wcmtechnologies/public_html/Phone/TheFinalTouchSecurity/TwilioAPI/twilio-php-master/Services/Twilio.php:297
如果我添加 &#34;至&#34; =&GT; &#34;一些十位数的电话号码&#34;, &#34;从&#34; =&GT; &#34;一些十位数的电话号码&#34;, 到数组,不会抛出错误。然后将呼叫定向到&#34; To&#34;电话号码。如果在&#34; To&#34;中指定的电话号码参数答案,然后调用已连接,并且forwardToBob.xml中的twiml同时执行所有操作。
EDIT#3 -------------------------------------------- --------------------------
这是我的整个代码......
这是每次调用Twilio验证号码时执行的Twiml。我从Twilio快速入门网站获得了此代码。
<?php
header('Content-type: text/xml');
$callerId = "+19012311158";
// put your default Twilio Client name here, for when a phone number isn't given
$number = "Bob";
// get the phone number from the page request parameters, if given
if (isset($_REQUEST['PhoneNumber'])) {
$number = htmlspecialchars($_REQUEST['PhoneNumber']);
}
// wrap the phone number or client name in the appropriate TwiML verb
// by checking if the number given has only digits and format symbols
if (preg_match("/^[\d\+\-\(\) ]+$/", $number)) {
$numberOrClient = "<Number>" . $number . "</Number>";
} else {
$numberOrClient = "<Client>" . $number . "</Client>";
}
?>
<Response>
<Dial callerId="<?php echo $callerId ?>">
<?php echo $numberOrClient ?>
</Dial>
</Response>
这是我主要从Twilio快速入门网站复制的客户端浏览器。
<?php
include 'TwilioAPI/twilio-php-master/Services/Twilio/Capability.php';
// put your Twilio API credentials here
$accountSid = 'XXXXXXXX';
$authToken = 'XXXXXXXX';
// put your Twilio Application Sid here
$appSid = 'XXXXXXXXXXXXX';
// put your default Twilio Client name here
$clientName = 'Bob';
// get the Twilio Client name from the page request parameters, if given
if (isset($_REQUEST['client'])) {
$clientName = $_REQUEST['client'];
}
$capability = new Services_Twilio_Capability($accountSid, $authToken);
$capability->allowClientOutgoing($appSid);
$capability->allowClientIncoming($clientName);
$token = $capability->generateToken();
?>
<!DOCTYPE html>
<html>
<head>
<title>Demo</title>
<script type="text/javascript"
src="//static.twilio.com/libs/twiliojs/1.2/twilio.min.js"></script>
<script type="text/javascript"
src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js">
</script>
<link href="http://static0.twilio.com/bundles/quickstart/client.css"
type="text/css" rel="stylesheet" />
<script type="text/javascript">
var callSid = "";
Twilio.Device.setup("<?php echo $token; ?>");
Twilio.Device.ready(function (device) {
$("#log").text("Client '<?php echo $clientName ?>' is ready");
});
Twilio.Device.error(function (error) {
$("#log").text("Error: " + error.message);
});
Twilio.Device.connect(function (conn) {
callSid = conn.parameters.CallSid;
$("#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
conn.accept();
});
Twilio.Device.presence(function (pres) {
if (pres.available) {
// create an item for the client that became available
$("<li>", {id: pres.from, text: pres.from}).click(function () {
$("#number").val(pres.from);
call();
}).prependTo("#people");
}
else {
$("#" + pres.from).remove();
}
});
function call() {
// get the phone number or client to connect the call to
params = {"PhoneNumber": $("#number").val()};
Twilio.Device.connect(params);
}
function forward() {
var xmlhttp = new XMLHttpRequest();
params = "?CallSid=" + callSid + "&ForwardTo=" + document.getElementById("number").value;
xmlhttp.open("POST","forward.php" + params,false);
xmlhttp.send();
document.getElementById("log").innerHTML=xmlhttp.responseText;
}
function hangup() {
Twilio.Device.disconnectAll();
}
</script>
</head>
<body>
<button class="call" onclick="call();">
Call
</button>
<button class="hangup" onclick="hangup();">
Hangup
</button>
<input type="text" id="number" name="number"
placeholder="Enter a phone number or client to call"/>
<button class="call" onclick="forward();">
Forward
</button>
<div id="log">Loading pigeons...</div>
<ul id="people"/>
</body>
</html>
这是通过我的forward()函数的HTTP POST请求调用的转发代码。
<?php
// Get the PHP helper library from twilio.com/docs/php/install
require_once('TwilioAPI/twilio-php-master/Services/Twilio.php'); // Loads the library
// Your Account Sid and Auth Token from twilio.com/user/account
$sid = 'XXXXXX';
$token = 'XXXXXX';
$callSid = $_POST['CallSid'];
$client = new Services_Twilio($sid, $token);
// Get an object from its sid. If you do not have a sid,
// check out the list resource examples on this page
$call = $client->account->calls->get($callSid);
$call->update(array(
"Url" => "http://ftstoo.com/Phone/TheFinalTouchSecurity/forwardToBob.xml",
"Method" => "POST"
));
第一个代码段显示调用901-231-1158时执行的Twiml。然后将其发送给客户&#34; Bob&#34;。一旦连接成功,我按下我添加的前进按钮。这个前进按钮调用转发功能,然后向PHP脚本发出HTTP POST请求,这是最后一个片段。执行时,如果我没有&#34; To&#34;和&#34;来自&#34;在数组中为更新函数指定的参数,我收到一个错误。关于我应该尝试解决这个问题的任何建议都会有所帮助!
请注意:我在HTML中创建的按钮称为Forward,它调用我创建的forward()函数。我正在调用Sid并将其保存在实例变量中。我在Twilio.Device.Connect函数中检索此值。
完整的堆栈跟踪。
Stack trace:
#0 /home/wcmtechnologies/public_html/Phone/TheFinalTouchSecurity/TwilioAPI/twilio-php-master/Services/Twilio.php(180): Base_Services_Twilio->_processResponse(Array)
#1 /home/wcmtechnologies/public_html/Phone/TheFinalTouchSecurity/TwilioAPI/twilio-php-master/Services/Twilio/InstanceResource.php(31): Base_Services_Twilio->createData('/2010-04-01/Acc...', Array)
#2 /home/wcmtechnologies/public_html/Phone/TheFinalTouchSecurity/forward.php(22): Services_Twilio_InstanceResource->update(Array)
#3 {main}
thrown in /home/wcmtechnologies/public_html/Phone/TheFinalTouchSecurity/TwilioAPI/twilio-php-master/Services/Twilio.php on line 297
[12-Jun-2015 00:19:39 UTC] PHP Fatal error: Uncaught exception 'Services_Twilio_RestException' with message 'No 'To' number is specified' in /home/wcmtechnologies/public_html/Phone/TheFinalTouchSecurity/TwilioAPI/twilio-php-master/Services/Twilio.php:297
答案 0 :(得分:1)
Twilio开发者传道者在这里。
感谢所有细节,我设法将您尝试做的大部分内容放在一起,以便找到错误。我从来没有收到过你的错误信息,所以请记住这一点。
我发现我必须更改代码的以下部分才能使其正常工作。
在您的XHR请求中,停止尝试同步发出请求(我使用的是Firefox并且已弃用),只需使用:
xmlhttp.open("POST","forward.php" + params);
我也没有从我的PHP中的XHR请求中获取调用sid,因此我将$_POST
更改为$_REQUEST
并开始工作。
最后,客户端的呼叫sid与始发呼叫sid不同。这是父母的电话,您可以使用twilio-php helper library:
这样抓住它$callSid = $_REQUEST['CallSid'];
$client = new Services_Twilio($sid, $token);
// Get an object from its sid. If you do not have a sid,
// check out the list resource examples on this page
$call = $client->account->calls->get($callSid);
$parentCall = $client->account->calls->get($call->parent_call_sid);
$parentCall->update(array(
"Url" => "http://ftstoo.com/Phone/TheFinalTouchSecurity/forwardToBob.xml",
"Method" => "POST"
));
然后,您需要更新父呼叫以将其转发到原始URL并挂断到客户端。
我希望这有帮助!