Twilio忽略了PHP库中的错误

时间:2013-11-14 16:38:58

标签: php error-handling twilio

我有问题。我已经在PHP中设置了Twilio帮助程序库来发送短信,一切正常。但我需要做点什么,我不知道该怎么做。

以下是工作代码:

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);
        }

一切正常。问题是由于手机损坏而发生错误,通过阵列的循环中断并发送停止。我需要继续发送,我怎么能这样做?这是控制器,请求是从ajax调用发送的,这就是为什么有echo语句!

2 个答案:

答案 0 :(得分:4)

异常抛出时,foreach循环中断并执行到catch

因此,请将您的代码设为

try {
            $errorIds = array(); //user ids array which had broken phones
            $count = 0;
            foreach($listUsers as $user){
                try
                {
                $sms = $service->account->sms_messages->create(
                    $fromPhone, 
                    $user['phone'], // From user phone array
                    $message
                );
                }
                catch (Exception $e)
                {  //on error push userId in to error array
                    $count++;
                    array_push($errorIds, $user['userId']);
                }
                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);
        }

try catch block循环中的其他foreach

答案 1 :(得分:0)

这是相同的代码,但有点修剪......这对我有用!

<?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);

?>