简体   繁体   English

Twilio语音邮件PHP接口-不会在AWS EC2或Lightsail上运行

[英]Twilio Voicemail PHP Interface - Will not run on AWS EC2 or Lightsail

I'm attemting to build an web interface to check voicemail left for Twilio numbers. 我打算构建一个Web界面,以检查语音邮件是否包含Twilio号码。 I'm really stumped at this point was would appreciate any hints as to what I've missed. 在这一点上,我真的很沮丧,很高兴收到关于我错过的任何提示。 I am new to developement work so I may have very well be overlooking something simple. 我是开发工作的新手,所以我很可能会忽略一些简单的事情。 The code is running on an AWS LAMP VPS. 该代码在AWS LAMP VPS上运行。

1) index.php pulls the numbers from my twilio account, with a view details option for each number. 1)index.php从我的twilio帐户中提取数字,并为每个数字提供查看详细信息选项。

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "https://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title></title>
</head>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">

<!-- jQuery library -->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

<!-- Latest compiled JavaScript -->
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
<body>
    <div class="container">
        <div class="row">
        </div>
        <hr>
        <div class="row">
            <div class="col-md-12">
                <table class="table table-bordered" id="example">
                    <thead>
                        <tr>
                            <th>Sid</th>
                            <th>Friendly Name</th>
                            <th>Phone Number</th>
                            <th>Action</th>
                        </tr>
                    </thead>
                    <tbody>
                        <?php
                        // Get the PHP helper library from https://twilio.com/docs/libraries/php
                        require_once 'vendor/autoload.php'; // Loads the library
                        use Twilio\Rest\Client;
                        include("config.php");

                        $client = new Client($__sid, $__token);


                        foreach ($client->incomingPhoneNumbers->read() as $number) {
                        ?>
                        <tr>
                            <td><?php echo $number->sid;?></td>
                            <td><?php echo $number->friendlyName;?></td>
                            <td><?php echo $number->phoneNumber;?></td>

                            <td>
                                <a href="getphn_detail.php?sid=<?php echo $number->phoneNumber;?>">
                                    <button type="button" class="btn btn-primary">Detail</button>
                                </a>
                            </td>
                        </tr>
                        <?php } ?>
                </table>
            </div>
        </div>
    </div>
</body>
</html>

<script>
    $(document).ready(function() {
        $('#example').DataTable();
    });
</script>

2) When the details button is clicked the getphn_detail.php should be displaying all the call records with an option to download or delete a voicemail if there is one associated with that call record. 2)单击详细信息按钮时,getphn_detail.php应该显示所有呼叫记录,并且如果有与该呼叫记录相关的语音邮件,则可以选择下载或删除语音邮件。 I get an error 500, the apache error log is showing the below error. 我收到错误500,apache错误日志显示以下错误。 Line 129 is the last line of the file. 第129行是文件的最后一行。

[Thu May 17 16:44:07.266066 2018] [proxy_fcgi:error] [pid 2000:tid 140124901693184] [client xxxx:54504] AH01071: Got error 'PHP message: PHP Parse error: syntax error, unexpected end of file in /opt/bitnami/apache2/htdocs/getphn_detail.php on line 129\\n' [2018年5月17日星期四16:44:07.266066] [proxy_fcgi:error] [pid 2000:tid 140124901693184] [client xxxx:54504] AH01071:出现错误'PHP消息:PHP Parse错误:语法错误,/中意外的文件结尾129行上的opt / bitnami / apache2 / htdocs / getphn_detail.php \\ n'

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "https://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title></title>
</head>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">

<!-- jQuery library -->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

<!-- Latest compiled JavaScript -->
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
<body>
    <div class="container">
        <div class="row">
            <h1>
                Showing Details of <?=$_REQUEST["sid"]?>
                <span style="float:right"><a href="index.php">Back</a></span>
            </h1>
        </div>
        <hr>
        <div class="row">
            <div class="col-md-12">
                <table class="table table-bordered" id="example">
                    <thead>
                        <tr>
                            <th>Callid </th>
                            <th>From </th>
                            <th>To</th>
                            <th>Date</th>
                            <th>Duration</th>
                            <th>Action</th>
                        </tr>
                    </thead>
                    <tbody>
                        <?php
                        // Get the PHP helper library from https://twilio.com/docs/libraries/php
                        require_once 'vendor/autoload.php'; // Loads the library
                        use Twilio\Rest\Client;

                        include("config.php");

                        $client = new Client($__sid,$__token);

                        $phn = $_GET['sid'];
                        $calls= $client->calls->read(
                            array("to" => $phn)
                        );
                        // Loop over the list of calls and echo a property for each one
                        //print_r($calls);


                        $url="https://api.twilio.com/2010-04-01/Accounts/XXXXXXXXXXXXXXXXXXXXXXXXXXXXX/Recordings.json";

                        $username="XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX";
                        $password="XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX";
                        $ch = curl_init();
                        curl_setopt($ch, CURLOPT_URL, $url);
                        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
                        curl_setopt($ch, CURLOPT_USERPWD, "$username:$password");
                        curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
                        $output = curl_exec($ch);
                        $info = curl_getinfo($ch);
                        curl_close($ch);


                        $data = json_decode($output, true);
                        //print_r($data);
                        $recordings =array();

                        foreach($data['recordings'] as $key1 ) {
                            $recordings[][$key1['call_sid']]=$key1['sid']."~~".$key1['duration'];
                        }




                        foreach ($calls as $call) {

                        ?>
                        <tr>
                            <td><?php echo $call->sid;?></td>
                            <td><?php echo $call->from;?></td>
                            <td><?php echo $call->to;?></td>
                            <td>
                                <?php $array = json_decode(json_encode($call->startTime), true);
                                echo date('d-m-Y',strtotime($array['date']));?>
                            </td>

                            <td><?php echo $call->duration?></td>
                            <td>
                                <?php
                                foreach($recordings as $key => $record)
                                {
                                    foreach($record as $key2 => $value)
                                    {
                                        if($key2==$call->sid)
                                        {
                                            $data1=explode("~~",$value);
                                ?>
                                <a href="https://api.twilio.com/2010-04-01/Accounts/XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX/Recordings/<?=$data1[0]?>.mp3?Download=true"><button type="button" class="btn btn-primary">Recording (<?=$data1[1]?> Sec)</button></a>
                                <a href="deleterecord.php?id=<?=$data1[0]?>" onclick="return confirm('Are you sure you want to delete?');"><button type="button" class="btn btn-danger">Delete</button></a><BR />&nbsp;<BR />
                                <?
                                        }
                                    }

                                }
                                ?>
                        </tr>
                        <?php } ?>
                </table>
            </div>
        </div>
    </div>
</body>
</html>

<script>
    $(document).ready(function() {
        $('#example').DataTable();
    });
</script>

Just to clarify a few things for you, so that you and others might avoid this issue in the future: 只是为您澄清一些事情,以便您和其他人将来可以避免此问题:

  • You had an unexpected syntax error 您遇到了意外的语法错误
  • I took the code in question and ran php -l filename.php Your file validated, so I could say it was not a missing begin or end block or semicolon. 我接受了有问题的代码并运行了php -l filename.php您的文件已通过验证,因此我可以说它不是缺少的开始或结束块或分号。
  • I also noted, that you were intermixing tag types which isn't a great idea. 我还指出,您正在混合标记类型,这不是一个好主意。 I suspected that it was possibly an issue with short_open_tag = off however, php has been changed so that the tag <?= is available even without short_open_tag = on . 我怀疑short_open_tag = off可能是个问题,但是php已更改,因此即使没有short_open_tag = on也可以使用标记<?= This has been the case since php version 5.4. 自php 5.4版以来就是这种情况。 With that said, we never got to the point of verification of your short_open_tag setting. 话虽如此,我们还是无法验证您的short_open_tag设置。

As I'm guessing you know 我猜你知道

<?= $somevar ?>

Is the equivalent of 等于

<?php echo somevar; ?>

I know that you were using <?= and this, depending on version, should not be a problem with the php version you were using. 我知道您使用的是<?=而这取决于版本,您使用的php版本应该不是问题。 Using just <? 仅使用<? is a problem, and it seems that is where you had a problem and the parser was confused, but to that point, you were using <?php , <? 是一个问题,看来这是您遇到问题的地方,而解析器感到困惑,但是到那时,您正在使用<?php<? and <?= which is where lack of consistency ends up causing issues. <?= ,这是缺乏一致性最终导致问题的地方。

When doing a lot of intermixing of PHP and HTML as you were, I understand the attraction of <?= , and the support for it by PHP. 当您像以前那样将PHP和HTML进行大量混合时,我了解<?=的吸引力以及PHP对它的支持。
I also advocate that if you have chosen to use PHP for its inherent templating capabilities, then also use the Alternative Syntax for Control Structures . 我还主张,如果您选择使用PHP来实现其固有的模板功能,那么还请使用“ 控制结构替代语法” There is alternative syntax for if, while, for, foreach, and switch. if,while,for,foreach和switch还有其他语法。

When intermixing with html, these fit better and are more readable surrounding and intermixing HTML as you are doing. 与html混合时,它们更适合并且在您进行操作时围绕HTML和混合HTML更易读。

In reality, short open tags have come to be officially frowned upon, and are now turned off by default, but in most cases where someone has a PHP based application, are not going to conflict with anything or cause problems. 实际上,短打开标记已被正式皱眉,并且现在默认情况下处于关闭状态,但是在大多数情况下,如果有人拥有基于PHP的应用程序,则不会与任何内容冲突或引起问题。 I'm not advising to go against the grain, but just acknowledging the reality. 我不建议违背事实,而只是承认现实。 About the only thing short_open_tag = on causes a problem with is, if you are using PHP to output an xml file and you are intermixing the xml with PHP as in: 关于short_open_tag = on唯一引起问题的是,如果您使用PHP输出xml文件,并且将xml与PHP混合,如下所示:

<?xml version="1.0" ?>
<? //Some php ?>

Even that has a simple workaround, but it's an edge case for sure. 即使这有一个简单的解决方法,但肯定是一个极端情况。

In general you could have avoided this issue just by being consistent in your use of tags, and consistency is a good way to improve the overall quality of your code. 通常,仅通过一致使用标签就可以避免此问题,而一致性是提高代码整体质量的好方法。

My last piece of advice is that with heavy templating, there are a number of well established php template libraries that could help you separate your logic from your output. 我的最后一条建议是,通过大量的模板制作,有许多完善的php模板库可以帮助您将逻辑与输出分开。 In MVC, this would be separating the C(ontrol) from the V(iew) in your case. 在MVC中,根据您的情况,这会将C(ontrol)与V(iew)分开。 Template engines that are worth looking at are Laravel's Blade, Symfony's Twig, Smarty and Plates. 值得一看的模板引擎是Laravel的Blade,Symfony的Twig,Smarty和Plates。

By moving all your markup to a template file the control/logic portions of your code would be concise and simple. 通过将所有标记移至模板文件,代码的控件/逻辑部分将变得简洁明了。 There is still a good deal of logic in your view, since you are doing looping, but all the template engines I listed have features that let you do that looping on data structures, and provide little helpers for manipulating the data. 由于您正在执行循环,因此在您看来仍然有很多逻辑,但是我列出的所有模板引擎都具有让您在数据结构上进行循环的功能,并且几乎没有帮助处理数据的工具。

For example here is a bit of twig template code that loops over an array: 例如,下面是一些遍历数组的树枝模板代码:

<!-- templates/homepage.twig -->
<h1>{{ pageTitle }}</h1>

<div class="row">
    {% for product in products %}
        <div class="span4">
            <h2>{{ product }}</h2>
        </div>
    {% endfor %}
</div> 

I took that from a free lesson by the folks at KNPUniversity. 我从KNPUniversity的一个免费课程中学到了这一点。 The principle of KNPU is the lead documentation person for the symfony project. KNPU的原则是symfony项目的主要文档编制人。 If you like the material the full twig course is reasonably priced, but you will learn quite a lot, including something about the aforementioned alternative control structures just by reading through the material attached to the course page. 如果您喜欢这种材料,那么完整的树枝课程价格合理,但是您将学到很多东西,包括通过阅读附在课程页面上的材料而学到的有关上述其他控制结构的知识。

Beyond the solution to your configuration problem, I hope that this might help you with refactoring and improving your project and those you embark on in the future. 除了解决配置问题之外,我希望这可以帮助您重构和改进您的项目以及将来从事的项目。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM