yii2vn / esms
ESMS服务的API客户端
Requires
- yiisoft/yii2: *
- yiisoft/yii2-httpclient: ~2.0.0
This package is not auto-updated.
Last update: 2024-09-29 01:32:58 UTC
README
这是一个用于与ESMS服务的API交互的组件。此组件支持您与以下API进行交互:
- 发送短信。
- 发送语音通话。
- 检查您的系统余额。
- 通过消息id检查消息状态。
- 通过消息id检查接收者列表。
要了解更多关于API的信息,请参阅ESMS的文档这里
安装
您必须通过composer安装此应用程序
安装composer后,执行以下命令
php composer.phar require --prefer-dist yii2vn/esms
注意:对于使用
window
的用户,请执行以下命令composer require --prefer-dist yii2vn/esms
或者
"yii2vn/esms": "*"
添加到composer.json中
配置应用
您可以在系统配置(应用配置)中声明此component
,以便重复使用而无需再次声明apiKey
和secretKey
。
return [ 'components' => [ 'eSMS' => [ 'class' => 'yii2vn\esms\ESMS', 'apiKey' => 'API KEY lấy từ ESMS', 'secretKey' => 'SECRET KEY lấy từ ESMS' ] ] ]
配置完成后,您就可以通过Yii::$app->eSMS
与API交互。
发送短信的方式
以下是内部方法的介绍
1. 向一个电话号码发送短信
使用sendSMS
方法,您可以向任何电话号码发送短信
$phone = '0909113911'; $esms = Yii::$app->eSMS; if ($esms->sendSMS($phone, 'Xin chao')) { Yii::$app->session->setFlash('Success!'); } else { Yii::warning('Không thể gửi tin đến sđt: ' . $phone . ' Lỗi: ' . $esms->error); }
2. 向多个电话号码发送相同内容的短信
除了sendSMS
方法外,组件还支持batchSendSMS
方法,用于在多个电话号码上发送短信
$phones = ['0909113911', '0909911113', '0909123456']; $esms = Yii::$app->eSMS; if ($esms->batchSendSMS($phones, 'Xin chao')) { Yii::$app->session->setFlash('Success!'); } else { Yii::warning('Không thể gửi tin đến sđt: ' . $phone . ' Lỗi: ' . $esms->error); }
3. 向多个电话号码发送不同内容的短信
使用batchSendSMS
方法,可以将具有键为电话号码的数组作为phones
传入,其中值为要发送的消息
$phones = [ '0909113911' => 'Ngay mai hop luc 8h', '0909911113' => 'Thu 2 lam ca sang', '0909123456' => '01/05 duoc nghi lam' ]; $esms = Yii::$app->eSMS; if ($esms->batchSendSMS($phones)) { Yii::$app->session->setFlash('Success!'); } else { Yii::warning('Không thể gửi tin đến sđt: ' . $phone . ' Lỗi: ' . $esms->error); }
4. 在指定时向多个电话号码发送不同内容的短信
使用batchSendSMS
的另一种方式是,phones
数组可以有键为电话号码,值为要发送的消息。而那些值是电话号码的元素将一起发送同一短信
$phones = [ '0909113911' => 'Ngay mai nhan vien se hop luc 8h! Kinh moi sep tham gia cuoc hop', // Số của sếp '0909911113', '0909123321', '0909963147' ]; $esms = Yii::$app->eSMS; if ($esms->batchSendSMS($phones, 'Ngay mai moi nguoi du hop vao luc 8h! Co sep den du.')) { Yii::$app->session->setFlash('Success!'); } else { Yii::warning('Không thể gửi tin đến sđt: ' . $phone . ' Lỗi: ' . $esms->error); }
如您所见,此时第二个参数(param 2)将携带值是共同发送给那些没有键的电话号码的短信。
发送语音通话的方式
以下是内部方法的介绍
1. 向一个电话号码发送语音通话
使用sendVoiceCall
方法,您可以向任何电话号码发送语音通话
$phone = '0909113911'; $apiCode = '58888'; // lấy trên hệ thống ESMS để xác định mẫu cuộc gọi thoại $passCode = '12345'; // lấy trên hệ thống ESMS để xác định mẫu cuộc gọi thoại $esms = Yii::$app->eSMS; if ($esms->sendVoiceCall($phone, $apiCode, $passCode, 'Xin chao')) { Yii::$app->session->setFlash('Success!'); } else { Yii::warning('Không thể gửi tin đến sđt: ' . $phone . ' Lỗi: ' . $esms->error); }
2. 向多个电话号码发送相同内容的语音通话
除了sendVoiceCall
方法外,组件还支持batchSendVoiceCall
方法,用于在多个电话号码上发送短信
$phones = ['0909113911', '0909911113', '0909123456']; $apiCode = '58888'; // lấy trên hệ thống ESMS để xác định mẫu cuộc gọi thoại $passCode = '12345'; // lấy trên hệ thống ESMS để xác định mẫu cuộc gọi thoại $esms = Yii::$app->eSMS; if ($esms->batchSendVoiceCall($phones, $apiCode, $passCode, 'Xin chao')) { Yii::$app->session->setFlash('Success!'); } else { Yii::warning('Không thể gửi tin đến sđt: ' . $phone . ' Lỗi: ' . $esms->error); }
3. 向多个电话号码发送不同内容的语音通话
使用batchSendVoiceCall
的另一种方式是,将具有键为电话号码的数组作为phones
传入,其中值为要发送的通话内容
$phones = [ '0909113911' => 'Ngay mai hop luc 8h', '0909911113' => 'Thu 2 lam ca sang', '0909123456' => '01/05 duoc nghi lam' ]; $apiCode = '58888'; // lấy trên hệ thống ESMS để xác định mẫu cuộc gọi thoại $passCode = '12345'; // lấy trên hệ thống ESMS để xác định mẫu cuộc gọi thoại $esms = Yii::$app->eSMS; if ($esms->batchSendVoiceCall($phones, $apiCode, $passCode)) { Yii::$app->session->setFlash('Success!'); } else { Yii::warning('Không thể gửi tin đến sđt: ' . $phone . ' Lỗi: ' . $esms->error); }
4. 在指定时向多个电话号码发送不同内容的语音通话
另一种使用 batchSendVoiceCall
方法的方式是,数组 phones
可以有一个主键是电话号码,值是想要发送的通话内容。而那些值是电话号码的组成部分将被发送相同的通话内容。
$phones = [ '0909113911' => 'Ngay mai nhan vien se hop luc 8h! Kinh moi sep tham gia cuoc hop', // Số của sếp '0909911113', '0909123321', '0909963147' ]; $apiCode = '58888'; // lấy trên hệ thống ESMS để xác định mẫu cuộc gọi thoại $passCode = '12345'; // lấy trên hệ thống ESMS để xác định mẫu cuộc gọi thoại $esms = Yii::$app->eSMS; if ($esms->batchSendVoiceCall($phones, $apiCode, $passCode, 'Ngay mai moi nguoi du hop vao luc 8h! Co sep den du.')) { Yii::$app->session->setFlash('Success!'); } else { Yii::warning('Không thể gửi tin đến sđt: ' . $phone . ' Lỗi: ' . $esms->error); }
正如你所看到的,此时第二个参数(param 2)将携带一个共同的通话内容值,发送到数组中没有键的电话号码上,它的值正是电话号码。
状态检查方法
从 ESMS
API 的发送短信 sms
和发送电话呼叫 voice call
方法返回的所有结果都会附带一个组件 SmsId
,请保存它,并在需要时使用它来检查状态。以下是如何从 SmsId
检查状态的方法
1. 检查短信(sms)或电话呼叫(voice call)的状态
$esms = Yii::$app->eSMS; $smsId = '123-421-412412-123'; $statusData = $esms->getSendStatus($smsId); var_dump($statusData); // Kết quả: // array(7) { ["CodeResponse"]=> string(3) "100" ["SMSID"]=> string(39) "123-421-412412-123" ["SendFailed"]=> int(0) ["SendStatus"]=> int(5) ["SendSuccess"]=> int(1) ["TotalReceiver"]=> int(1) ["TotalSent"]=> int(1) }
2. 检查接收者列表和短信(sms)或电话呼叫(voice call)的状态
$esms = Yii::$app->eSMS; $smsId = '123-421-412412-123'; $statusData = $esms->getReceiverStatus($smsId); var_dump($statusData); // Kết quả: // array(2) { ["CodeResult"]=> string(3) "100" ["ReceiverList"]=> array(1) { [0]=> array(3) { ["IsSent"]=> bool(true) ["Phone"]=> string(10) "0909113911" ["SentResult"]=> bool(true) } } }
余额检查方法
要检查余额,请使用 getBalance
方法。该方法有一个默认参数(param)$force
,在获取数据后,balance
组件将 cache
以供后续调用使用。如果您想强制API重新获取数据,请将此参数设置为 TRUE
。
$esms = Yii::$app->eSMS; var_dump($esms->getBalance()); // Kết quả: // array(3) { ["Balance"]=> int(2293566) ["CodeResponse"]=> string(3) "100" ["UserID"]=> int(999999999999) }
$force
的示例
$esms = Yii::$app->eSMS; var_dump($esms->getBalance()); // Kết quả: // array(3) { ["Balance"]=> int(2293566) ["CodeResponse"]=> string(3) "100" ["UserID"]=> int(999999999999) } $esms->sendSMS("0909113911", "hello"); var_dump($esms->getBalance()); // Kết quả số dư vẫn như cữ: // array(3) { ["Balance"]=> int(2293566) ["CodeResponse"]=> string(3) "100" ["UserID"]=> int(999999999999) } var_dump($esms->getBalance(true)); // kết quả sẽ thay đổi vì chúng ta lấy lại dữ liệu sau khi gửi tin => số tiền sẽ giảm
API 反馈结果
与 sendSMS
、sendVoiceCall
、getBalance
方法相对应,如果 ResponseCode
或 ResultCode
为 100
,则将返回根据 ESMS
API 文档定义的结果数组。如果 ResponseCode
或 ResultCode
不是 100
,则返回 FALSE
。要检索错误,请使用 getError
方法或 error
属性来获取发送过程中的最后一个错误。
与 batchSendSMS
、batchSendVoiceCall
方法相对应,将返回一个结果数组集合,其键是发送短信或电话呼叫的电话号码,值是返回的结果数组或如果结果数组中的 ResponseCode
或 ResultCode
不是 100
,则返回 FALSE
。
事件
这些事件是为了在发送消息或电话呼叫之前和之后以及执行一些辅助任务而创建的。
以下是 ESMS
类内部的事件列表
EVENT_BEFORE_SEND_SMS
发送短信前的事件EVENT_AFTER_SEND_SMS
发送短信后的事件EVENT_BEFORE_SEND_VOICE_CALL
发送电话呼叫前的事件EVENT_AFTER_SEND_VOICE_CALL
发送电话呼叫后的事件