0

我正在关注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 包含一个带有说动词的响应。此 php 代码(不是 twiml)引发错误

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

如果我将“To”=>“Some Ten Digit Phone Number”、“From”=>“Some Ten Digit Phone Number”添加到数组中,则不会引发错误。然后将呼叫定向到“收件人”电话号码。如果“To”参数中指定的电话号码应答,则呼叫已连接,并且 forwardToBob.xml 中的 twiml 将同时执行。

编辑#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。然后将其定向到客户端“Bob”。一旦连接成功,我按下我添加的转发按钮。这个转发按钮调用转发函数,然后向作为最后一个片段的 PHP 脚本发出 HTTP POST 请求。执行时,如果我没有在数组中为更新函数指定“To”和“From”参数,则会收到错误消息。关于我应该尝试解决这个问题的任何建议都会非常有帮助!

请注意:我在 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
4

1 回答 1

1

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

// 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 并在客户端挂断。

我希望这有帮助!

于 2015-06-15T11:19:50.147 回答