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