本文介绍了将调用重定向到新的 Twiml URL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在关注 https://www 上的教程.twilio.com/docs/api/rest/change-call-state#post 我在 php 中编码了允许您将当前入站呼叫转发到新 Twiml URL 的部分.我发现为了使其工作,我必须在更新数组中指定一个 To 和 From 参数.我需要将呼叫转发到指定的 URL,而不是 To 参数中指定的数字.但是,Twilio API 会抛出一个错误,指出 To 参数是必需的,但文档表明它不是.我在这里做错了什么吗?

calls->get($callSid);$call->更新(数组(网址" =>"http://ftstoo.com/Phone/TheFinalTouchSecurity/forwardToBob.xml",方法"=>邮政"));?>

forwardToBob.xml 包含一个带有说动词的响应.这个 php 代码(不是 twiml)抛出错误

在/home/wcmtechnologies/public_html/Phone/TheFinalTouchSecurity/TwilioAPI/twilio-php-master/Services/Twilio.php:297 中未捕获的异常Services_Twilio_RestException"和消息未指定收件人"号码"

如果我添加"To" => "一些十位数的电话号码","来自" => "一些十位数的电话号码",到数组,不会抛出错误.然后将呼叫定向到收件人"电话号码.如果To"参数中指定的电话号码应答,则呼叫已连接并且 forwardToBob.xml 中的 twiml 同时执行.

编辑#3----------------------------------------------------------------------

这是我的全部代码....

这是每次调用 Twilio 验证号码时执行的 Twiml.我从 Twilio 快速入门站点获得了此代码.

 ".$数字."</数字>";} 别的 {$numberOrClient = "".$数字."</客户端>";}?><回应><Dial callerId="<?php echo $callerId ?>"><?php echo $numberOrClient ?></拨号></响应>

这是我主要从 Twilio 快速入门站点复制的客户端浏览器.

allowClientIncoming($clientName);$token = $capability->generateToken();?><!DOCTYPE html><头><title>演示</title><脚本类型=文本/javascript"src="//static.twilio.com/libs/twiliojs/1.2/twilio.min.js"></script><脚本类型=文本/javascript"src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"><link href="http://static0.twilio.com/bundles/quickstart/client.css"type="text/css" rel="样式表"/><script type="text/javascript">var callSid = "";Twilio.Device.setup("<?php echo $token; ?>");Twilio.Device.ready(功能(设备){$("#log").text("Client '<?php echo $clientName ?>' is ready");});Twilio.Device.error(函数(错误){$("#log").text("错误:" + error.message);});Twilio.Device.connect(函数(连接){callSid = conn.parameters.CallSid;$("#log").text("成功建立通话");});Twilio.Device.disconnect(功能(连接){$("#log").text("通话结束");});Twilio.Device.incoming(函数(连接){$("#log").text("来自" + conn.parameters.From 的传入连接);//接受传入连接并启动双向音频连接.接受();});Twilio.Device.presence(function (pres) {如果(预可用){//为可用的客户端创建一个项目$("
  • ", {id: pres.from, text: pres.from}).click(function () {$("#number").val(pres.from);称呼();}).prependTo("#people");}别的 {$("#" + pres.from).remove();}});函数调用(){//获取电话号码或客户端将呼叫连接到params = {"电话号码": $("#number").val()};Twilio.Device.connect(params);}函数转发(){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;}函数挂断(){Twilio.Device.disconnectAll();}<身体><button class="call" onclick="call();">称呼<button class="hangup" onclick="hangup();">挂断<input type="text" id="number" name="number"placeholder="输入要呼叫的电话号码或客户"/><button class="call" onclick="forward();">向前<div id="log">正在加载鸽子...</div><ul id="人"/></html>
  • 这是通过来自我的 forward() 函数的 HTTP POST 请求调用的转发代码.

    calls->get($callSid);$call->更新(数组(网址" =>"http://ftstoo.com/Phone/TheFinalTouchSecurity/forwardToBob.xml",方法"=>邮政"));

    第一个代码片段显示了在调用 901-231-1158 时执行的 Twiml.然后将其定向到客户端Bob".连接成功后,我按下我添加的转发按钮.这个转发按钮调用转发函数,然后向 PHP 脚本发出 HTTP POST 请求,这是最后一个片段.执行时,如果我没有在数组中为更新函数指定To"和From"参数,我会收到一个错误.任何有关我应该尝试解决此问题的建议都会非常有帮助!

    请注意:我在 HTML 中创建的按钮称为 Forward,它调用我创建的函数 forward().我收到呼叫 Sid 并将其保存在一个实例变量中.我在 Twilio.Device.Connect 函数中检索此值.

    完整的堆栈跟踪.

    堆栈跟踪:#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 ...', 大批)#2/home/wcmtechnologies/public_html/Phone/TheFinalTouchSecurity/forward.php(22): Services_Twilio_InstanceResource->update(Array)#3 {主要}在第 297 行的/home/wcmtechnologies/public_html/Phone/TheFinalTouchSecurity/TwilioAPI/twilio-php-master/Services/Twilio.php 中抛出[2015 年 6 月 12 日 00:19:39 UTC] PHP 致命错误:在/home/wcmtechnologies/public_html/Phone/TheFinalTouchSecurity/TwilioAPI/twilio-php 中未捕获异常Services_Twilio_RestException"并带有消息未指定收件人"号码"-master/Services/Twilio.php:297
    解决方案

    Twilio 开发人员布道者在这里.

    感谢您提供的所有详细信息,为了找出错误,我设法将您尝试执行的大部分操作汇总在一起.不过,我从未收到过您收到的错误消息,所以请记住这一点.

    我发现我必须更改您代码的以下部分才能使其正常工作.

    在您的 XHR 请求中,停止尝试同步发出请求(我使用的是 Firefox,但它已被弃用),只需使用:

    xmlhttp.open("POST","forward.php" + params);

    我也没有从 PHP 的 XHR 请求中获取调用 sid,所以我将 $_POST 更改为 $_REQUEST 并开始工作.

    最后,客户端的呼叫 sid 与发起呼叫的 sid 不同.这是父调用,您可以像这样使用 twilio-php 辅助库:

    $callSid = $_REQUEST['CallSid'];$client = new Services_Twilio($sid, $token);//从它的 sid 中​​获取一个对象.如果你没有sid,//查看此页面上的列表资源示例$call = $client->account->calls->get($callSid);$parentCall = $client->account->calls->get($call->parent_call_sid);$parentCall->更新(数组(网址" =>"http://ftstoo.com/Phone/TheFinalTouchSecurity/forwardToBob.xml",方法"=>邮政"));

    然后您需要更新父调用以将其转发到您的原始 URL 并在客户端挂断.

    我希望这会有所帮助!

    I am following the tutorial on https://www.twilio.com/docs/api/rest/change-call-state#post I am coding in php the portion that allows you to forward a current inbound call to a new Twiml URL. I am finding that in order for this to work, I have to specify a To and From parameter in the update array. I need the call forwarded to the URL specified not the number specified in the To parameter. However, the Twilio API throws an error that says the To parameter is required but the docs indicate that it is not. Is there something I am doing wrong here?

    <?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 contains a Response with a Say Verb.This php code (not the twiml) throws the 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

    If I add "To" => "Some Ten Digit Phone Number", "From" => "Some Ten Digit Phone Number",to the array, the error is not thrown. The call is then directed to the "To" phone number. If the phone number specified in the "To" parameter answers, then the call is connected AND the twiml at forwardToBob.xml executes all at the same time.

    EDIT # 3----------------------------------------------------------------------

    Here is my entire code....

    This is the Twiml that is executed everytime the Twilio verified number is called. I got this code from the Twilio quickstart site.

        <?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>
    

    This is the client browser which I mostly copied from the Twilio quickstart site.

    <?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>
    

    This is the forwarding code that is called via an HTTP POST request from my forward() function.

    <?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"
        ));
    

    The first code snippet shows the Twiml that is executed when 901-231-1158 is called. It is then directed to the Client "Bob". Once the connection is successful, I press the forward button which I added in. This forward button calls the forward function which then makes an HTTP POST request to the PHP script which is the last snippet. Upon executing, if I do not have the "To" and "From" parameters specified in the array for the update function, I receive an error. Any suggestions on what I should try to fix this would help very much!

    PLEASE NOTE: The button that I created in HTML is called Forward and it calls the function forward() which I created. I am getting the call Sid and saving it in an instance variable. I retrieve this value in the Twilio.Device.Connect function.

    The full stack trace.

    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
    
    解决方案

    Twilio developer evangelist here.

    Thanks for all the details, I managed to put together most of what you were trying to do in order to find the errors. I never received the error message you had though, so bare that in mind.

    I found I had to change the following parts of your code to get this to work.

    In your XHR request, stop trying to make the request synchronously (I was using Firefox and it was deprecated), just use:

    xmlhttp.open("POST","forward.php" + params);
    

    I was also not getting the call sid from the XHR request in my PHP, so I changed $_POST to $_REQUEST and that started to work.

    Finally, the call sid on the client side is not the same as the originating call sid. That is the parent call and you can get hold of it like this using the 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"
    ));
    

    You then need to update the parent call to forward it on to your original URL and hang up on the client side.

    I hope this helps!

    这篇关于将调用重定向到新的 Twiml URL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

    08-20 04:50