Twilio PHP错误处理不适用于SMS

时间:2019-10-03 00:26:17

标签: php twilio twilio-api twilio-php

我在Tronio的php cron中使用Rest API。 我想做的是遍历数据库中所有需要文本消息的记录,然后向他们发送文本。

代码工作正常,但是如果引发错误,脚本将崩溃。我以为我在try / catch中正确使用了Exception,但是它似乎没有抓住任何错误并继续sms循环。

.content-slider.owl-carousel .owl-item,
.content-slider.owl-carousel  .owl-item:active {
cursor: default;
}
 .owl-animated-in {
-webkit-animation-delay: 1s;
-moz-animation-delay: 1s;
-o-animation-delay: 1s;
-ms-animation-delay: 1s;
animation-delay: 1s;
}
.owl-item p, .owl-item h5, .owl-item h1 {
-webkit-backface-visibility: hidden;
backface-visibility: hidden;
}
.animated.bounceIn, .animated.bounceOut, .animated.flipOutX, 
.animated.flipOutY {
animation-duration: 0.6s;
-webkit-backface-visibility: hidden;
backface-visibility: hidden;
}

我确定还有其他方法可以清除此代码,但是我需要首先捕获并处理错误。如您所见,一旦发送了文本,我会将其记录在数据库表中以供程序使用。只要没有错误,所有这些都可以正常工作。现在,如果我“退订”自己的SMS,此cron.php页面将显示HTTP 500服务器错误,从而中断SMS循环!

下面的更新

在我的本地服务器上,出现此错误:

<?php
chdir(dirname(__FILE__)); //need this line so cron works! cron doesn't know the relative file paths otherwise.
require_once 'core/init.php';
require 'vendor/autoload.php';

// Use the REST API Client to make requests to the Twilio REST API
use Twilio\Rest\Client;

// Your Account SID and Auth Token from twilio.com/console
$sid = 'mySid';
$token = 'myToken';
$client = new Client($sid, $token);

$db = DB::getInstance();

$appts = DB::getInstance()->query("SELECT appointments.id, contacts.id AS contact_id, CONCAT(contacts.first_name, ' ', contacts.last_name) AS contact_name, contacts.cell_phone, appointments.start AS appt_time, locations.name AS location_name, locations.phone AS location_phone, locations.sms_from_number, companies.name AS company_name
FROM 
appointments 
LEFT JOIN contacts ON contacts.id = appointments.contact_id
LEFT JOIN locations ON locations.id = appointments.location_id
LEFT JOIN companies ON companies.id = appointments.company_id
WHERE appt_status_id IN (2,9,10) AND 
(DATE(`start`) = CURDATE() + INTERVAL 1 DAY) AND locations.sms_appt_reminders = 'Y' AND contacts.cell_phone IS NOT NULL AND contacts.cell_phone <> '' AND appointments.allDay = 0");

if ($appts->error()) {
    echo 'Error occurred.'; 
} else {

    if ($appts->results()) {

        foreach($appts->results() AS $result) {

            $date = date_create($result->appt_time);
            //remove spaces, remove parentheses, hyphen, add +1 US country code.
            $formatted_cell_number = str_replace(' ', '', $result->cell_phone);
            $formatted_cell_number = str_replace('(', '', $formatted_cell_number);
            $formatted_cell_number = str_replace(')', '', $formatted_cell_number);
            $formatted_cell_number = str_replace('-', '', $formatted_cell_number);
            $formatted_cell_number = '+1' . $formatted_cell_number;

            try {

                // Use the client to send text messages!
                $client->messages->create(
                    $formatted_cell_number,
                    array(
                        'from' => $result->sms_from_number,
                        'body' => 'You have an appointment at ' . $result->company_name . ' (' . $result->location_name . ' office) tomorrow at ' . date_format($date, 'g:i A') . '. Please respond \'C\' to confirm this appointment. Please reply \'R\' if you need to reschedule, or call us at ' . $result->location_phone
                    )
                );

                DB::getInstance()->query("INSERT INTO sms_notifications (contact_id, sms_to_number, sms_from_number, sms_message, category_id, appt_id) VALUES ('".$result->contact_id."', '".$formatted_cell_number."', '".$result->sms_from_number."', 'Contact TEXTED to confirm appoinment on:  ".$result->appt_time."', '0', '".$result->id."')");

            } catch(Exception $e) {

                echo $e->getStatus()."<br>";

            }

        }

    }
}

?>

第59行是echo'error:'。 $ e-> getStatus()。“
”;

我注意到在API的Exceptions文件夹中有一个包含的php页面,是这样的:

Fatal error: Uncaught Error: Call to undefined method Twilio\Exceptions\EnvironmentException::getStatus() in C:\Users\miche\Google Drive\Server\htdocs\test\cron_sms_appt_reminders.php:59 Stack trace: #0 {main} thrown in C:\Users\me\Google Drive\Server\htdocs\test\cron_sms_appt_reminders.php on line 59

1 个答案:

答案 0 :(得分:0)

似乎在我的实时服务器上可以将$ e-> getStatus()更改为$ e-> getStatusCode()。万一有人碰到这个问题,那就是正确捕获错误的窍门。