Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions publish/sms.php
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,16 @@
],
],

'itexmo' => [
'driver' => \HyperfExt\Sms\Drivers\ItexmoDriver::class,
'config' => [
'email' => '',
'password' => '',
'api_code' => '',
'sender_id' => '',
],
],

'log' => [
'driver' => \HyperfExt\Sms\Drivers\LogDriver::class,
'config' => [
Expand Down
63 changes: 63 additions & 0 deletions src/Drivers/ItexmoDriver.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
<?php


namespace HyperfExt\Sms\Drivers;


use HyperfExt\Sms\Contracts\SmsableInterface;
use HyperfExt\Sms\Exceptions\DriverErrorException;
use HyperfExt\Sms\Exceptions\RequestException;

class ItexmoDriver extends AbstractDriver
{
/**
* 验证码 - 即时发送
*/
protected const SERVER = 'https://api.itexmo.com/api/broadcast-otp';

public function __construct(array $config)
{
if (empty($config['email']) || empty($config['password']) || empty($config['api_code']) || empty($config['sender_id'])) {
throw new \InvalidArgumentException('SMS 配置信息不全!');
}
parent::__construct($config);
}

public function send(SmsableInterface $smsable): array
{
if (empty($smsable->to)) {
throw new \InvalidArgumentException('Empty phone number');
}

if (empty($smsable->content)) {
throw new \InvalidArgumentException('Empty message');
}

$recipient = $smsable->to->getCountryCode() === 63 ? $smsable->to->getNationalNumber() : $smsable->to->getFullNumberWithIDDPrefix('PH');
$data = [
'Email' => $this->config->get('email'),
'Password' => $this->config->get('password'),
'Recipients' => [$recipient],
'Message' => $smsable->content,
'ApiCode' => $this->config->get('api_code'),
'SenderId' => $this->config->get('sender_id'),
];

try {
$response = $this->client->postJson(self::SERVER, $data);
$result = $response->toArray();

if ($result['Error'] != false) {
throw new DriverErrorException($result['Message'], $result['Code'] ?? -1, $response);
}

return $result;
} catch (RequestException $e) {
$response = $e->getResponse();

return $response->toArray();
} catch (\Throwable $e) {
throw new DriverErrorException($e->getMessage(), $e->getCode());
}
}
}