Twilio黑名单规则致命错误
问题描述:
我正在使用twilio发送批量短信。假设有些客户认为他们不想再收到消息,所以他们回复“停止”,并将其添加到黑名单中。我很难编码电话号码,因为我仍然在自己的手机上进行测试。我注意到,当我没有从我的代码中删除黑名单上的数字时,我收到一条错误消息,并且我的脚本停止在该点。Twilio黑名单规则致命错误
将来,我可能会使用存储在数据库或文件中的数字。在那种情况下,如果发生这种情况,我该如何克服这个问题。基本上我想要做的是:如果一个号码在黑名单中,转到下一个号码,并避免使用异常或类似的错误。 错误消息和代码如下。
谢谢,
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<title>Send SMS</title>
<?php
/* Send an SMS using Twilio. You can run this file 3 different ways:
*
* 1. Save it as sendnotifications.php and at the command line, run
* php sendnotifications.php
*
* 2. Upload it to a web host and load mywebhost.com/sendnotifications.php
* in a web browser.
*
* 3. Download a local server like WAMP, MAMP or XAMPP. Point the web root
* directory to the folder containing this file, and load
* localhost:8888/sendnotifications.php in a web browser.
*/
// Step 1: Get the Twilio-PHP library from twilio.com/docs/libraries/php,
// following the instructions to install it with Composer.
//require_once "vendor/autoload.php";
require __DIR__ . '/twilio-php-master/Twilio/autoload.php';
use Twilio\Rest\Client;
// Step 2: set our AccountSid and AuthToken from https://twilio.com/console
$AccountSid = "something";
$AuthToken = "something";
// Step 3: instantiate a new Twilio Rest Client
$client = new Client($AccountSid, $AuthToken);
// Step 4: make an array of people we know, to send them a message.
// Feel free to change/add your own phone number and name here.
$people = array(
"+1757" => "Chris",
"+17571234568" => "Hussam"
);
// Step 5: Loop over all our friends. $number is a phone number above, and
// $name is the name next to it
foreach ($people as $number => $name) {
$sms = $client->account->messages->create(
// the number we are sending to - Any phone number
$number,
array(
// Step 6: Change the 'From' number below to be a valid Twilio number
// that you've purchased
'from' => "+184444444444",
// the sms body
'body' => "Hey $name, this is Hussam. Testing Twilio SMS API!"
)
);
// Display a confirmation message on the screen
echo "Sent message to $name.\n";
}
?>
严重错误(!):未捕获的异常 'Twilio \例外\ RestException' 与消息“[HTTP 400]无法创建记录:消息从/到对违反了黑名单规则“。在第86行(!)Twilio \ Exceptions \ RestException:[HTTP 400]中的C:\ wamp64 \ www \ Twilio \ twilio-php-master \ Twilio \ Version.php无法创建记录:邮件从/到对违反了黑名单规则。在C:\ wamp64 \ WWW \ Twilio \ twilio-PHP-主\ Twilio \ Version.php上线86调用堆栈
时间记忆功能位置1 0.0000 239280 {主}()... \ send.php: ()... \ send.php:56 3 0.0156 814688 Twilio \ Version-> create()... \ MessageList.php:63
答
Twilio开发人员在这里传播。
您需要捕获从发送消息到黑名单号码的请求中引发的异常。你可以用try
和catch
这样做:
foreach ($people as $number => $name) {
try {
$sms = $client->account->messages->create(
$number,
array(
'from' => "+18443949780",
'body' => "Hey $name, this is Hussam. Testing Twilio SMS API!"
)
);
echo "Sent message to $name.\n";
} catch (Exception $e) {
echo "Couldn't send message to $number\n";
}
}
当你钩这份长达一个数据库,你会想,这样你不使用catch
更新场以纪念号受阻试着再次发送给它。
让我知道这是否有帮助。
太棒了!有效!!谢谢.. –
没问题!祝好运与您的其他应用程序! – philnash
你在开玩笑吗?当某人被列入黑名单时,为什么你会崩溃?你不能只是返回一个错误代码?这是非常不专业的。 –