我有一个 php 脚本,它允许我将 OTP 发送到 SMS Api;但有时短信会延迟或未送达,所以我正在考虑在代码中添加另一个 Api,这样现在 OTP 也会发送到 WhatsApp。
谁能帮我修改一下代码,在下面的代码中添加另一个api吗?
$phone = preg_replace('/[^0-9]/', '', $phone);
$curl = curl_init();
$curl = curl_init();
$params = array(
'Username' => '030XXXXXX',
'Password' => 'XXXXXX',
'From' => 'XXXXXX',
'To' => $phone,
'Message' => $message,
);
$encoded_query = http_build_query($params);
curl_setopt($curl, CURLOPT_URL, 'https://connect.jazz.com/sendsms_url.html?' . $encoded_query);
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
$result = curl_exec($curl);
$code = curl_getinfo($curl, CURLINFO_HTTP_CODE);
$curl_error = curl_errno($curl);
curl_close($curl);
if($test_call) return $result;
if ($curl_error !== 0) {
return false;
}
if ($err) {
return false;
} else {
return true;
}
}
谢谢你
使用策略模式来实施您的短信提供商。然后,您可以发送带有作业的消息,当作业失败时,使用其他策略(如 WhatsApp 等提供商)重试。
处理失败情况的示例作业:
class SendExampleSmsJob implements ShouldQueue
{
use InteractsWithQueue, Queueable, SerializesModels;
public function handle()
{
// Process sending message ...
}
/**
* Handle a job failure.
*/
public function failed(Throwable $exception)
{
// dispatch another job to send Whatsapp message or something else
}
}