Twilio PHP 错误处理不适用于 SMS
Twilio PHP Error Handing Not Working for SMS
我在我的 php cron 中为 Twilio 使用 Rest API。
我想做的是遍历我数据库中需要文本消息的所有记录,然后向它们发送文本。
代码工作正常,但如果抛出错误,脚本就会崩溃。我以为我在 try/catch 中正确使用了异常,但它似乎没有捕捉到任何错误并继续 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>";
}
}
}
}
?>
我确定还有其他方法可以清理此代码,但我需要先捕获并处理错误。如您所见,发送文本后,我将其记录在数据库 table 中供我的程序使用。只要没有错误,所有这些都可以正常工作。现在,如果我 'unsubscribe' 我自己从 SMS,这个 cron.php 页面显示 HTTP 500 服务器错误,打破了 SMS 循环!
以下更新
在我的本地服务器上出现此错误:
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
第 59 行是 echo 'error: '。 $e->getStatus()."
";
我注意到 API 的 Exceptions 文件夹中包含一个 php 页面,它是这样的:
namespace Twilio\Exceptions;
class RestException extends TwilioException {
protected $statusCode;
/**
* Construct the exception. Note: The message is NOT binary safe.
* @link http://php.net/manual/en/exception.construct.php
* @param string $message [optional] The Exception message to throw.
* @param int $code [optional] The Exception code.
* @param int $statusCode [optional] The HTTP Status code.
* @since 5.1.0
*/
public function __construct($message, $code, $statusCode) {
$this->statusCode = $statusCode;
parent::__construct($message, $code);
}
/**
* Get the HTTP Status Code of the RestException
* @return int HTTP Status Code
*/
public function getStatusCode() {
return $this->statusCode;
}
}
似乎将 $e->getStatus() 更改为 $e->getStatusCode() 可以在我的实时服务器上运行。万一有人遇到这个问题,那就是正确捕获错误的技巧。
我在我的 php cron 中为 Twilio 使用 Rest API。 我想做的是遍历我数据库中需要文本消息的所有记录,然后向它们发送文本。
代码工作正常,但如果抛出错误,脚本就会崩溃。我以为我在 try/catch 中正确使用了异常,但它似乎没有捕捉到任何错误并继续 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>";
}
}
}
}
?>
我确定还有其他方法可以清理此代码,但我需要先捕获并处理错误。如您所见,发送文本后,我将其记录在数据库 table 中供我的程序使用。只要没有错误,所有这些都可以正常工作。现在,如果我 'unsubscribe' 我自己从 SMS,这个 cron.php 页面显示 HTTP 500 服务器错误,打破了 SMS 循环!
以下更新
在我的本地服务器上出现此错误:
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
第 59 行是 echo 'error: '。 $e->getStatus()."
";
我注意到 API 的 Exceptions 文件夹中包含一个 php 页面,它是这样的:
namespace Twilio\Exceptions;
class RestException extends TwilioException {
protected $statusCode;
/**
* Construct the exception. Note: The message is NOT binary safe.
* @link http://php.net/manual/en/exception.construct.php
* @param string $message [optional] The Exception message to throw.
* @param int $code [optional] The Exception code.
* @param int $statusCode [optional] The HTTP Status code.
* @since 5.1.0
*/
public function __construct($message, $code, $statusCode) {
$this->statusCode = $statusCode;
parent::__construct($message, $code);
}
/**
* Get the HTTP Status Code of the RestException
* @return int HTTP Status Code
*/
public function getStatusCode() {
return $this->statusCode;
}
}
似乎将 $e->getStatus() 更改为 $e->getStatusCode() 可以在我的实时服务器上运行。万一有人遇到这个问题,那就是正确捕获错误的技巧。