2016-12-06 55 views
3

我正在使用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

回答

2

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更新場以紀念號受阻試着再次發送給它。

讓我知道這是否有幫助。

+0

太棒了!有效!!謝謝.. –

+0

沒問題!祝好運與您的其他應用程序! – philnash

+0

你在開玩笑嗎?當某人被列入黑名單時,爲什麼你會崩潰?你不能只是返回一個錯誤代碼?這是非常不專業的。 –