php - Twilio忽略PHP库中的错误

标签 php error-handling twilio

我有个问题。我已经在PHP中设置了Twilio帮助程序库来发送SMS,并且一切正常。但是我需要做一些事情,但我不知道该怎么做。

这是工作代码:

try {
            $errorIds = array(); //user ids array which had broken phones
            $count = 0;
            foreach($listUsers as $user){
                $sms = $service->account->sms_messages->create(
                    $fromPhone, 
                    $user['phone'], // From user phone array
                    $message
                );
                if(!$sms){  //on error push userId in to error array
                    $count++;
                    array_push($errorIds, $user['userId']);
                } else {
                    $count=0;
                }
                if($count>20){ //if 20 errors in row give back errors
                    $data['results'] = "error";
                    $data['message'] = "Encountered to many failed messages in row";
                    $data['error_id_array'] = $errorIds;
                    $data['error_id'] = $user['userId'];
                    echo json_encode($data);
                }

            }
            $data['results'] = "success";
            $data['message'] = "Your message have been sent successfully";
            $data['error_id_array'] = $errorIds;
            echo json_encode($data);
       } catch (Services_Twilio_RestException $e) { 
            $data['results'] = "error";
            $data['message'] = $e->getMessage();
            $data['error_id'] = $user['userId'];
            echo json_encode($data);
        }

一切正常。问题是当由于手机损坏而发生错误时,遍历阵列的循环会中断,并且发送停止。我需要继续发送,该怎么办?这是 Controller ,请求是从ajax调用发送的,这就是为什么有echo语句的原因!

最佳答案

这是相同的代码,但经过了一些整理……对我有用!

<?PHP

    require "Services/Twilio.php";

    // Set our AccountSid and AuthToken from twilio.com/user/account
    $AccountSid = "{ACCOUNTSID}";
    $AuthToken = "{AUTHTOKEN}";

    // Instantiate a new Twilio Rest Client
    $client = new Services_Twilio($AccountSid, $AuthToken);

    /* Your Twilio Number or Outgoing Caller ID */
    $from = '2126404004';

    $people = array("212-716-1130"); 

    $body = "Enter your text message here";

    $errorIds = array(); //user ids array which had broken phones

    foreach ($people as $to) { 
        try
        {
        $client->account->sms_messages->create($from, $to, $body);
        echo "Sent message to: $to \n <br>";

        }
        catch (Exception $e)
        {  //on error push userId in to error array
            $count++;
            array_push($errorIds, $to);
        }
    }

    print_r($errorIds);

?>

关于php - Twilio忽略PHP库中的错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19983262/

相关文章:

android - Twilio android sdk 增加了应用程序大小 36 MB 可以减少吗?

php - MYSQL ON DUPLICATE KEY 插入其他内容

c# - 我想念 C# 中 Visual Basic 的 "On Error Resume Next"。我现在应该如何处理错误?

c++ - 断言和 NDEBUG

.net - 使用自定义异常类抛出多个异常

swift - 不支持的 IOSurface 格式 : 0x26424741 using twilio video in scenekit

php - jquery 告诉我某些内容已添加到表中,但事实并非如此

php - 比较多个数组值并更新表状态

php - MySQL 2 个表中的多个 WHERE 条件

java.lang.NoClassDefFoundError : com/twilio/sdk/TwilioRestClient