gpt4 book ai didi

Twilio 黑名单规则 fatal error

转载 作者:行者123 更新时间:2023-12-01 00:36:15 25 4
gpt4 key购买 nike

我正在使用 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(
"+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";
}
?>

( ! ) fatal error :未捕获异常“Twilio\Exceptions\RestException”,消息为“[HTTP 400] 无法创建记录:消息发件人/收件人对违反黑名单规则。”在 C:\wamp64\www\Twilio\twilio-php-master\Twilio\Version.php 上的第 86 行(!)Twilio\Exceptions\RestException:[HTTP 400] 无法创建记录:来自/至对的消息违反了黑名单规则。在 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->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 (\Twilio\Exceptions\RestException $e) {
echo "Couldn't send message to $number\n";
}
}
当你把它连接到数据库时,你会想要使用 catch更新字段以将号码标记为已阻止,以便您不会再次尝试向其发送。
让我知道这是否有帮助。

关于Twilio 黑名单规则 fatal error ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40997801/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com