本文实例讲述了laravel框架使用阿里云短信发送消息操作。,具体如下:
最新需要用到发送短信的功能,所以就在网上搜索一些写好的扩展。
扩展地址:
https://github.com/MissMyCat/aliyun-sms
通过composer安装:
composer require mrgoon/aliyun-sms dev-master
在 config/app.php 中 providers 加入:
MrgoonAliSmsServiceProvider::class,
有需求的可以自行添加 aliases。
然后在控制台运行 :
php artisan vendor:publish
默认会在 config 目录下创建一个 aliyunsms.php 文件:
<?php
return [
'access_key' => env('ALIYUN_SMS_AK'), // accessKey
'access_secret' => env('ALIYUN_SMS_AS'), // accessSecret
'sign_name' => env('ALIYUN_SMS_SIGN_NAME'), // 签名
];
然后在 .env 中配置相应参数:
ALIYUN_SMS_AK= ALIYUN_SMS_AS= ALIYUN_SMS_SIGN_NAME=
为了能够方便的发送短信,我们可以在 app 目录下,创建一个Services目录,并添加 AliyunSms.php 文件。
<?php
namespace AppServices;
use MrgoonAliSmsAliSms;
/**
* 阿里云短信类
*/
class AliyunSms
{
//验证码
const VERIFICATION_CODE = 'verification_code';
//模板CODE
public static $templateCodes = [
self::VERIFICATION_CODE => 'SMS_XXXXXXXXXX',
];
/**
* 发送短信
*/
public static function sendSms($mobile, $scene, $params = [])
{
if (empty($mobile)) {
throw new Exception('手机号不能为空');
}
if (empty($scene)) {
throw new Exception('场景不能为空');
}
if (!isset(self::$templateCodes[$scene])) {
throw new Exception('请配置场景的模板CODE');
}
$template_code = self::$templateCodes[$scene];
try {
$ali_sms = new AliSms();
$response = $ali_sms->sendSms($mobile, $template_code, $params);
if ($response->Code == 'OK') {
return true;
}
throw new Exception($response->Message);
} catch (Throwable $e) {
throw new Exception($e->getMessage());
}
}
}
为了能够记录每次短信发送的状态,我们可以创建一个 sms_logs 表。
CREATE TABLE `sms_logs` ( `id` int(11) unsigned NOT NULL AUTO_INCREMENT COMMENT 'ID', `type` tinyint(1) NOT NULL DEFAULT '0' COMMENT '类型(0:短信验证码,1:语音验证码,2:短信消息通知)', `mobile` varchar(16) NOT NULL DEFAULT '' COMMENT '手机号', `code` varchar(12) NOT NULL DEFAULT '' COMMENT '验证码', `checked` tinyint(1) NOT NULL DEFAULT '0' COMMENT '是否验证(0:未验证,1:已验证)', `status` tinyint(1) NOT NULL DEFAULT '0' COMMENT '状态(0:未发送,1:已发送,2:发送失败)', `reason` varchar(255) NOT NULL DEFAULT '' COMMENT '失败原因', `remark` varchar(255) NOT NULL DEFAULT '' COMMENT '备注', `operator_id` int(11) NOT NULL DEFAULT '0' COMMENT '操作人ID', `ip` varchar(16) NOT NULL DEFAULT '' COMMENT '操作IP', `created` int(11) NOT NULL DEFAULT '0' COMMENT '创建时间', `updated` int(11) NOT NULL DEFAULT '0' COMMENT '更新时间', PRIMARY KEY (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='短信表';







