簡體   English   中英

Twilio語音郵件PHP接口-不會在AWS EC2或Lightsail上運行

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

我打算構建一個Web界面,以檢查語音郵件是否包含Twilio號碼。 在這一點上,我真的很沮喪,很高興收到關於我錯過的任何提示。 我是開發工作的新手,所以我很可能會忽略一些簡單的事情。 該代碼在AWS LAMP VPS上運行。

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)單擊詳細信息按鈕時,getphn_detail.php應該顯示所有呼叫記錄,並且如果有與該呼叫記錄相關的語音郵件,則可以選擇下載或刪除語音郵件。 我收到錯誤500,apache錯誤日志顯示以下錯誤。 第129行是文件的最后一行。

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

只是為您澄清一些事情,以便您和其他人將來可以避免此問題:

  • 您遇到了意外的語法錯誤
  • 我接受了有問題的代碼並運行了php -l filename.php您的文件已通過驗證,因此我可以說它不是缺少的開始或結束塊或分號。
  • 我還指出,您正在混合標記類型,這不是一個好主意。 我懷疑short_open_tag = off可能是個問題,但是php已更改,因此即使沒有short_open_tag = on也可以使用標記<?= 自php 5.4版以來就是這種情況。 話雖如此,我們還是無法驗證您的short_open_tag設置。

我猜你知道

<?= $somevar ?>

等於

<?php echo somevar; ?>

我知道您使用的是<?=而這取決於版本,您使用的php版本應該不是問題。 僅使用<? 是一個問題,看來這是您遇到問題的地方,而解析器感到困惑,但是到那時,您正在使用<?php<? <?= ,這是缺乏一致性最終導致問題的地方。

當您像以前那樣將PHP和HTML進行大量混合時,我了解<?=的吸引力以及PHP對它的支持。
我還主張,如果您選擇使用PHP來實現其固有的模板功能,那么還請使用“ 控制結構替代語法” if,while,for,foreach和switch還有其他語法。

與html混合時,它們更適合並且在您進行操作時圍繞HTML和混合HTML更易讀。

實際上,短打開標記已被正式皺眉,並且現在默認情況下處於關閉狀態,但是在大多數情況下,如果有人擁有基於PHP的應用程序,則不會與任何內容沖突或引起問題。 我不建議違背事實,而只是承認現實。 關於short_open_tag = on唯一引起問題的是,如果您使用PHP輸出xml文件,並且將xml與PHP混合,如下所示:

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

即使這有一個簡單的解決方法,但肯定是一個極端情況。

通常,僅通過一致使用標簽就可以避免此問題,而一致性是提高代碼整體質量的好方法。

我的最后一條建議是,通過大量的模板制作,有許多完善的php模板庫可以幫助您將邏輯與輸出分開。 在MVC中,根據您的情況,這會將C(ontrol)與V(iew)分開。 值得一看的模板引擎是Laravel的Blade,Symfony的Twig,Smarty和Plates。

通過將所有標記移至模板文件,代碼的控件/邏輯部分將變得簡潔明了。 由於您正在執行循環,因此在您看來仍然有很多邏輯,但是我列出的所有模板引擎都具有讓您在數據結構上進行循環的功能,並且幾乎沒有幫助處理數據的工具。

例如,下面是一些遍歷數組的樹枝模板代碼:

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

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

我從KNPUniversity的一個免費課程中學到了這一點。 KNPU的原則是symfony項目的主要文檔編制人。 如果您喜歡這種材料,那么完整的樹枝課程價格合理,但是您將學到很多東西,包括通過閱讀附在課程頁面上的材料而學到的有關上述其他控制結構的知識。

除了解決配置問題之外,我希望這可以幫助您重構和改進您的項目以及將來從事的項目。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM