'How to Send OTP to Multiple SMS Api?
I have a php script which allows me to send OTPs to an SMS Api; but sometimes the sms gets delayed or not delivered so I was thinking of adding another Api to the code so now the OTP is sent to WhatsApp as well.
Can any one help me modify the code to add another api in the following code?
$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;
}
}
Thank You
Solution 1:[1]
Use the strategy pattern to implement your SMS providers. Then you can send your messages with jobs and when the job was failed retry that with another strategy (provider like WhatsApp).
Example job to handle the failing situation:
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
}
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | A.Seddighi |
