Twilio黑名单规则致命错误

时间:2016-12-06 14:32:56

标签: twilio twilio-php twilio-api

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

(!)致命错误:未捕获异常'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-&gt; create()... \ send。 php:56 3 0.0156 814688 Twilio \ Version-&gt; create()... \ MessageList.php:63

2 个答案:

答案 0 :(得分:3)

Twilio开发者传道者在这里。

您需要捕获从请求中抛出的异常,以便将消息发送到列入黑名单的号码。您可以使用trycatch这样执行此操作:

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更新字段以将该号码标记为已阻止,以便您不再尝试向其发送

让我知道这是否有帮助。

答案 1 :(得分:0)

这对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());    
          }
     }