使用Twilio从浏览器拨打电话 - 呼叫自动终止

时间:2016-11-15 12:05:33

标签: php twilio twilio-php twilio-api

我试图通过我的twilio试用帐户拨打电话。我引用了这个link。基于此链接,我创建了一个名为 hello-client-twiml.php 的页面,其中包含以下代码:

<?php
header('Content-type: text/xml');

// put a phone number you've verified with Twilio to use as a caller ID number
$callerId = "+xxxxxxxxxx";

// put your default Twilio Client name here, for when a phone number isn't given
$number   = "jenny";

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

hello-client-monkey.php 页面,代码为:

<?php
include "vendor/autoload.php";

use Twilio\Jwt\ClientToken;

// put your Twilio API credentials here
$accountSid = 'your_sid_here';
$authToken  = 'your_auth_token';

// put your TwiML Application Sid here
$appSid = 'APXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX';

$capability = new ClientToken($accountSid, $authToken);
$capability->allowClientOutgoing($appSid);
$capability->allowClientIncoming('jenny');
$token = $capability->generateToken();
?>

<!DOCTYPE html>
<html>
  <head>
    <title>Hello Client Monkey 4</title>
    <script type="text/javascript"
      src="//media.twiliocdn.com/sdk/js/client/v1.3/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="//static0.twilio.com/resources/quickstart/client.css"
      type="text/css" rel="stylesheet" />
    <script type="text/javascript">

      Twilio.Device.setup("<?php echo $token; ?>");

      Twilio.Device.ready(function (device) {
        $("#log").text("Ready");
      });

      Twilio.Device.error(function (error) {
        $("#log").text("Error: " + error.message);
      });

      Twilio.Device.connect(function (conn) {
        $("#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();
      });

      function call() {
        // get the phone number to connect the call to
        params = {"PhoneNumber": $("#number").val()};
        Twilio.Device.connect(params);
      }

      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 to call"/>

    <div id="log">Loading pigeons...</div>
  </body>
</html>

在Twilio控制台TwiML apps page中,我已将http://mywebsite.com/hello-client-monkey.php添加为语音 - >请求网址

现在,当我运行页面 hello-client-monkey.php 时,调用将自动终止,并在twilio console logs我收到错误日志:

WARNING
   12200 Schema validation warning
DESCRIPTION
 Cannot find the declaration of element 'html'.

任何人都可以帮我解决这个问题吗?提前谢谢。

1 个答案:

答案 0 :(得分:0)

来自文档:

https://www.twilio.com/docs/api/errors/12200

  

架构验证警告

     

提供的XML不符合Twilio标记XML架构。   请参阅具体错误并更正问题。

     

可能的原因

     

拼写错误的动词,动词不正确,拼写错误或未知   属性,未知或意外的嵌套元素。

     

可能的解决方案

     

检查警告报告的行和列,看看是哪一部分   您的XML响应导致投诉

正如Devin所说,你的TwiML应用程序需要在 hello-client-twiml.php 中定义twiml。