我还尝试将break
右侧放在外面,然后在 try/catch 之后使用continue
里面的 catch 但没有运气。除了那里的异常之外,似乎没有其他异常被抛出。
do {
try {
$result = CM::campaigns()->create(config('campaignmonitor.client_id'), array_merge($optional_params, [
'Subject' => $campaign->subject,
'Name' => $campaign->client_id . ' - ' . $campaign->name . ' (' . $campaign->id . ')',
'FromName' => $campaign->from_name,
'FromEmail' => $campaign->from_email,
'ReplyTo' => (!is_null($campaign->reply_email) ? $campaign->reply_email : $campaign->from_email),
'HtmlUrl' => config('campaignmonitor.emailTemplatePath') . '/' . $campaign->html_file,
'SegmentIDs' => [$campaign->segment_id]
]));
break;
} catch (CurlException $e) {
// Try again
$cmCreationTries++;
}
} while ($cmCreationTries < 3);
以下确实按预期工作:
do {
try {
$result = CM::campaigns()->create(config('campaignmonitor.client_id'), array_merge($optional_params, [
'Subject' => $campaign->subject,
'Name' => $campaign->client_id . ' - ' . $campaign->name . ' (' . $campaign->id . ')',
'FromName' => $campaign->from_name,
'FromEmail' => $campaign->from_email,
'ReplyTo' => (!is_null($campaign->reply_email) ? $campaign->reply_email : $campaign->from_email),
'HtmlUrl' => config('campaignmonitor.emailTemplatePath') . '/' . $campaign->html_file,
'SegmentIDs' => [$campaign->segment_id]
]));
// Request was successful, we can end the loop.
$cmCreationTries = 4;
} catch (CurlException $e) {
// Try again
$cmCreationTries++;
}
} while ($cmCreationTries < 3);