1

我有个问题。我已经在 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);
        }

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

4

2 回答 2

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循环

于 2013-11-14T17:37:01.220 回答
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);

?>
于 2014-01-09T04:39:06.500 回答