mindertech / laravel-aliyun-dysms
适用于 Laravel 的阿里云短信 PHP SDK
1.0.1
2021-06-25 08:14 UTC
Requires
- php: >=7.1.13
- laravel/framework: >=5.5.5
Requires (Dev)
- phpunit/phpunit: ~4.0
This package is auto-updated.
Last update: 2024-09-25 16:05:48 UTC
README
-
将以下内容添加到您的 composer.json 文件中。然后运行
composer update"pgroot/laravel-aliyun-dysms": "dev-master"
或者
composer require pgroot/laravel-aliyun-dysms
-
运行以下命令以发布包配置文件
config/dysms.phpphp artisan vendor:publish --provider=Mindertech\Dysms\MindertechDysmsServiceProvider
配置
在 config/dysms.php 中设置属性值。
默认
return [ 'access_key_id' => '', 'access_key_secret' => '', 'sign' => '', 'log' => false, 'sms-report-queue' => '', 'sms-up-queue' => '', //以下配置暂时无需替换 'product' => 'Dysmsapi', 'domain' => 'dysmsapi.aliyuncs.com', 'region' => 'cn-hangzhou', 'end_point_name' => 'cn-hangzhou', 'mns' => [ 'account_id' => '1943695596114318', 'product' => 'Dybaseapi', 'domain' => 'dybaseapi.aliyuncs.com', 'wait_seconds' => 3 ], ];
用法
-
发送短信
try { $bizId = SendSms::to('SMS_123456', '18688886666', [ 'code' => mt_rand(1000, 9999) ]); } catch(\Exception $e) { echo $e->getMessage(); }
-
查询短信
$page = 1; $pageSize = 1; $bizId = null; $result = QuerySms::search('18688886666', '20180516', $page, $pageSize, $bizId);
响应
{ "page": 1, "pageSize": 1, "sendDate": "20180516", "result": [{ "SendDate": "2018-05-16 16:04:53", "SendStatus": 3, "ReceiveDate": "2018-05-16 16:04:57", "ErrCode": "0", "TemplateCode": "SMS_123456", "Content": "", "PhoneNum": "18688886666" }], "total": 7 } -
批量发送短信
try { $bizId = SendSmsBatch::to( 'SMS_123456', [ '18688886666', '18666666666' ], [ 'sign-1', 'sign-2' ], [ [ 'code' => mt_rand(1000, 9999) ], [ 'code' => mt_rand(1000, 9999) ] ] ); } catch (\Exception $e) { echo $e->getMessage(); }
-
MNS
查看:https://help.aliyun.com/document_detail/55500.html
/** * 回调 * @param stdClass $message 消息数据 * @return bool 返回true,则工具类自动删除已拉取的消息。返回false,消息不删除可以下次获取 */ SmsQueue::up(function($message) { /* { "dest_code": "2199787" "send_time": "2018-05-16 18:05:13" "sign_name": "signname" "sp_id": null "sequence_id": 531571249 "phone_number": "18688886666" "content": "回复测试" } */ print_r($message); return true; });
/** * 回调 * @param stdClass $message 消息数据 * @return bool 返回true,则工具类自动删除已拉取的消息。返回false,消息不删除可以下次获取 */ SmsQueue::report(function($message) { /* { "send_time": "2018-05-16 14:18:57" "report_time": "2018-05-16 14:19:02" "success": true "err_msg": "用户接收成功" "err_code": "DELIVERED" "phone_number": "18688886666" "sms_size": "1" "biz_id": "48490846451537371^0" "out_id": null } */ print_r($message); return true; });
-
在运行时设置配置
try { $bizId = SendSms::setRuntimeConfig([ 'access_key_id' => 'id', 'access_key_secret' => 'key' ])->to('SMS_123456', '18688886666', [ 'code' => mt_rand(1000, 9999) ]); } catch(\Exception $e) { echo $e->getMessage(); }