Need to use the new features to send text messages, so I search for some written extensions online.
Extended Address:
https://github.com/MissMyCat/aliyun-sms
Installation by composer:
composer require mrgoon/aliyun-sms dev-master
In config / app.php the providers added:
Mrgoon\AliSms\ServiceProvider::class,
There needs can add their own aliases.
Then the console run:
php artisan vendor:publish
Aliyunsms.php default creates a file in the config directory:
<?php return [ 'access_key' => env('ALIYUN_SMS_AK'), // accessKey 'access_secret' => env('ALIYUN_SMS_AS'), // accessSecret 'sign_name' => env('ALIYUN_SMS_SIGN_NAME'), // 签名 ];
.Env then disposed in the corresponding parameter:
ALIYUN_SMS_AK = ALIYUN_SMS_AS = ALIYUN_SMS_SIGN_NAME =
To be able to easily send text messages, we can in the app directory, create a Services Directory, and add AliyunSms.php file.
<PHP? namespace App \ Services; use Mrgoon \ AliSms \ AliSms; / ** * Ali cloud SMS class * / class AliyunSms { // code const VERIFICATION_CODE = 'VERIFICATION_CODE'; // template CODE public static $ templateCodes = [ Self VERIFICATION_CODE = ::> 'SMS_XXXXXXXXXX', ]; / ** * send message * / public static function sendSms (Mobile $, $ SCENE, the params = $ []) { IF (empty ($ Mobile)) { the throw new new \ Exception ( 'phone number can not be empty'); } IF (empty ($ sCENE)) { the throw new new \ Exception ( 'scenario can not be empty'); } 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()); } } }
To be able to record every SMS sent by the state, we can create a sms_logs table.
TABLE `yt_sms_logs` the CREATE ( ` id` int (. 11) the AUTO_INCREMENT the COMMENT unsigned the NOT NULL 'ID', `type` tinyint (1) the NOT NULL the DEFAULT '0' the COMMENT 'type (0: message authentication code, a: voice codes , 2: SMS message notification) ', `mobile` VARCHAR (16) the NOT NULL the DEFAULT' 'the COMMENT' phone number ', ` code` VARCHAR (12 is) the NOT NULL the DEFAULT' 'the COMMENT' codes', `checked` tinyint ( 1) nOT NULL DEFAULT '0' COMMENT ' verify (0: not verified, 1: verified)', `status` tinyint (1) nOT NULL DEFAULT '0' COMMENT ' state (0: not sent, 1: is sending, 2: transmission failure) ', `reason` VARCHAR (255) the NOT NULL the DEFAULT' 'the COMMENT' cause of failure ', ` remark` VARCHAR (255) the NOT NULL the DEFAULT' 'the COMMENT' Remarks', `operator_id` int (. 11 ) NOT NULL DEFAULT '0' COMMENT ' operator ID', `ip` varchar(16) NOT NULL DEFAULT '' COMMENT '操作IP', `created` int (11) NOT NULL DEFAULT '0' COMMENT ' Created', `updated` int (11) NOT NULL DEFAULT '0' COMMENT ' update', a PRIMARY KEY (` id`) ) ENGINE the InnoDB the DEFAULT the CHARSET = = = utf8mb4 the COMMENT 'message table';
Then for this table, we create a SmsLog models to manage.
? <PHP namespace the App \ Models; use the App \ Services \ AliyunSms; class SmsLog the extends the Model { protected $ Fillable = [ 'type', 'Mobile', 'code', 'the checked', 'Status', ' reason ', ' the remark ', ' operator_id ', ' IP ', ]; // type (0: message authentication code, a: voice verification code, 2: SMS message notification) const = 0 type_code; const TYPE_VOICE = 1; const TYPE_MESSAGE = 2; // verify (0: not verified, 1: verified) const = 0 CHECKED_UNVERIFIED; const CHECKED_VERIFIED = 1; // status (0: not sent, 1: sent, 2:Failed to send) STATUS_NO_SEND 0 = const; const = STATUS_SEND. 1; const STATUS_FAIL = 2; transmission time interval // messages, default 60 seconds const SEND_INTERVAL_TIME = 60; / ** * Detection SMS verification code * / protected function checkCode (Mobile $, $ code) { IF (! $ Mobile) { the throw new new \ Exception ( 'phone number can not be empty'); } (! checkMobile ($ Mobile)) IF { the throw new new \ Exception ( 'phone number is incorrect'); } IF (! code $) { the throw new new \ Exception ( 'codes can not be empty'); } $ sms_log = $ this-> WHERE ([ [ 'type', :: Self type_code], [ 'Mobile', $ Mobile], [ 'Status', Self :: STATUS_SEND], IF (!$mobile) { [ 'the checked', Self :: CHECKED_UNVERIFIED], ]) -> orderBy ( 'Created', 'desc') -> First (); IF ($ sms_log) {! the throw new new \ Exception ( 'codes do not exist, retrieve '); } IF ($ code = $ sms_log-> code) {! the throw new new \ Exception (' error codes'); } $ sms_log-> = Self :: CHECKED_VERIFIED the checked; $ sms_log-> Save () ; return to true; } / ** * SMS frequency detection * / protected function checkRate ($ Mobile) { } $ sms_log = $ this->where([ ['mobile', $mobile], throw new \ Exception ( 'phone number can not be empty'); [ 'Status', Self :: STATUS_SEND], ]) -> orderBy ( 'the Created', 'desc') -> First (); $ now = Time ( ); IF ($ sms_log) { IF ((now $ - strtotime ($ sms_log-> Created)) <Self :: SEND_INTERVAL_TIME) { the throw new new \ Exception ( 'SMS too frequently, please try again later'); } } return to true; } / ** * send message authentication code * / protected function sendVerifyCode ($ Mobile) { Self :: checkRate ($ Mobile); $ code = the mt_rand (1000, 9999); $ sms_log = $ this-> Create ([ 'type' => Self type_code ::, 'Mobile' => $ Mobile, 'code' => $code, 'checked' => self::CHECKED_UNVERIFIED, 'status' => self::STATUS_NO_SEND, 'ip' => getRealIp(), ]); try { AliyunSms::sendSms($mobile, AliyunSms::VERIFICATION_CODE, ['code' => $code]); $sms_log->status = self::STATUS_SEND; $sms_log->save(); return true; } catch (\Exception $e) { $sms_log->status = self::STATUS_FAIL; $sms_log->reason = $e->getMessage(); $sms_log->save(); throw new \Exception($e->getMessage()); } } }
In this way, we can just send text messages via SmsLog :: sendVerifyCode () in the project.