Twilio 黑名单规则致命错误
Twilio blacklist rule fatal error
我正在使用 twilio 发送群发短信。假设一些客户决定他们不想再接收消息,所以他们回复 "stop",这会将他们添加到黑名单中。我正在对 phone 数字进行硬编码,因为我仍在测试自己的 phone 单元格。我注意到,当我不从我的代码中删除黑名单上的数字时,我会收到一条错误消息,并且我的脚本会在那时停止。
将来,我可能会使用存储在数据库或文件中的数字。在那种情况下,如果它发生了,我该如何克服这个问题。基本上我想做的是:如果一个数字在黑名单中,移动到下一个数字并使用异常或其他东西避免该错误。
错误信息和代码如下。
谢谢,
<!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(
"+17570123456" => "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\Exceptions\RestException',消息为“[HTTP 400] 无法创建记录:消息 From/To 对违反了黑名单规则。”在 C:\wamp64\www\Twilio\twilio-php-master\Twilio\Version.php 第 86 行 (!) Twilio\Exceptions\RestException: [HTTP 400] 无法创建记录:消息 From/To 对违反了黑名单规则。在 C:\wamp64\www\Twilio\twilio-php-master\Twilio\Version.php 中第 86 行调用堆栈
时间记忆函数位置 1 0.0000 239280 {main}( ) ...\send.php:0 2 0.0156 799016 Twilio\Rest\Api\V2010\Account\MessageList->create() ...\send.php :56 3 0.0156 814688 Twilio\Version->创建( ) ...\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 (\Twilio\Exceptions\RestException $e) {
echo "Couldn't send message to $number\n";
}
}
当您将其连接到数据库时,您需要使用 catch
更新字段以将号码标记为已阻止,这样您就不会再次尝试发送给它。
如果有帮助请告诉我。
Laravel 5 这对我有用。注意 \Twilio\Exceptions\RestException.
的使用
try {
$sms = $client->account->messages->create(
$number,
array(
'from' => "+16136543180",
'body' => "Hey $name, Are you still mad at us about your cat!"
)
);
echo "Sent message to $name.\n";
} catch (\Twilio\Exceptions\RestException $e) {
if ($e->getCode() == 20404) {
//this will be false condition
dd('False Result 404');
} else {
//some other exception code
dd($e->getMessage());
}
}
我正在使用 twilio 发送群发短信。假设一些客户决定他们不想再接收消息,所以他们回复 "stop",这会将他们添加到黑名单中。我正在对 phone 数字进行硬编码,因为我仍在测试自己的 phone 单元格。我注意到,当我不从我的代码中删除黑名单上的数字时,我会收到一条错误消息,并且我的脚本会在那时停止。
将来,我可能会使用存储在数据库或文件中的数字。在那种情况下,如果它发生了,我该如何克服这个问题。基本上我想做的是:如果一个数字在黑名单中,移动到下一个数字并使用异常或其他东西避免该错误。 错误信息和代码如下。
谢谢,
<!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(
"+17570123456" => "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\Exceptions\RestException',消息为“[HTTP 400] 无法创建记录:消息 From/To 对违反了黑名单规则。”在 C:\wamp64\www\Twilio\twilio-php-master\Twilio\Version.php 第 86 行 (!) Twilio\Exceptions\RestException: [HTTP 400] 无法创建记录:消息 From/To 对违反了黑名单规则。在 C:\wamp64\www\Twilio\twilio-php-master\Twilio\Version.php 中第 86 行调用堆栈
时间记忆函数位置 1 0.0000 239280 {main}( ) ...\send.php:0 2 0.0156 799016 Twilio\Rest\Api\V2010\Account\MessageList->create() ...\send.php :56 3 0.0156 814688 Twilio\Version->创建( ) ...\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 (\Twilio\Exceptions\RestException $e) {
echo "Couldn't send message to $number\n";
}
}
当您将其连接到数据库时,您需要使用 catch
更新字段以将号码标记为已阻止,这样您就不会再次尝试发送给它。
如果有帮助请告诉我。
Laravel 5 这对我有用。注意 \Twilio\Exceptions\RestException.
的使用 try {
$sms = $client->account->messages->create(
$number,
array(
'from' => "+16136543180",
'body' => "Hey $name, Are you still mad at us about your cat!"
)
);
echo "Sent message to $name.\n";
} catch (\Twilio\Exceptions\RestException $e) {
if ($e->getCode() == 20404) {
//this will be false condition
dd('False Result 404');
} else {
//some other exception code
dd($e->getMessage());
}
}