PHP code example of abcsun / laravel-sms
1. Go to this page and download the library: Download abcsun/laravel-sms library . Choose the download type require .
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
abcsun / laravel-sms example snippets
//安装稳定版本
composer oser
//laravel 5.0.*
'Toplan\Sms\SmsManagerServiceProvider'
//laravel 5.1.*
Toplan\Sms\SmsManagerServiceProvider::class
//laravel 5.0.*
'SmsManager' => 'Toplan\Sms\Facades\SmsManager'
//laravel 5.1.*
'SmsManager' => Toplan\Sms\Facades\SmsManager::class
php artisan vendor:publish
php artisan migrate
'agent' => 'Luosimao',
> 'Luosimao' => [
> ...
> 'apikey' => 'your api key',
> ]
>
//只希望使用模板方式发送短信,可以不设置内容content (如云通讯,Submail)
Toplan\Sms\Sms::make($tempId)->to('1828****349')->data(['12345', 5])->send();
//只希望使用内容方式放送,可以不设置模板id和模板数据data (如云片,luosimao)
Toplan\Sms\Sms::make()->to('1828****349')->content('【Laravel SMS】亲爱的张三,欢迎访问,祝你工作愉快。')->send();
//同时确保能通过模板和内容方式发送。这样做的好处是,可以兼顾到各种代理器(服务商)!
Toplan\Sms\Sms::make([
'YunTongXun' => '123',
'SubMail' => '123'
])
->to('1828****349')
->data(['张三'])
->content('【签名】亲爱的张三,欢迎访问,祝你工作愉快。')
->send();
$sms = $sms->to('1828*******');
$sms = $sms->to(['1828*******', '1828*******', ...]);//多个目标号码
//静态方法设置,并返回sms实例
$sms = Toplan\Sms\Sms::make('20001');
//或
$sms = $sms->template('20001');
//静态方法设置,并返回sms实例
$sms = Toplan\Sms\Sms::make(['YunTongXun' => '20001', 'SubMail' => 'xxx', ...]);
//设置指定服务商的模板id
$sms = $sms->template('YunTongXun', '20001')->template('SubMail' => 'xxx');
//一次性设置多个服务商的模板id
$sms = $sms->template(['YunTongXun' => '20001', 'SubMail' => 'xxx', ...]);
$sms = $sms->data([
'code' => $code,
'minutes' => $minutes
]);//must be array
$sms = $sms->content('【签名】亲爱的张三,您的订单号是281xxxx,祝你购物愉快。');
$sms = $sms->openQueue();//开启队列,短信会在队列中排队
$sms = $sms->closeQueue();//关闭队列,短信会直接发送
$sms->send();//return true or false
//开启队列为true, 关闭队列为false
'smsSendQueue' => true,
php artisan queue:listen
'alternate' => [
//关闭备用代理器机制为false,打开为true
'enable' => false,
//备用代理器组,排名分先后,越在前面的代理器会优先使用
//example: ['YunPian', ...]
'agents' => []
],
> 'verifySmsContent' => '【填写签名】亲爱的用户,您的验证码是%s。有效期为%s分钟,请尽快验证'
>
> 'YunTongXun' => [
> //模板标示符
> 'verifySmsTemplateId' => 'your template id',
> ]
>
> 'rules' => [
> //唯一性检测规则
> 'check_mobile_unique' => 'unique:users,mobile',//适用于注册
> //存在性检测规则
> 'check_mobile_exists' => 'exists:users',//适用于找回密码和系统内业务验证
> ]
>
//验证手机验证码
$validator = Validator::make(Input::all(), [
'mobile' => ' ]);
if ($validator->fails()) {
//验证失败的话需要清除session数据,防止用户多次试错
\SmsManager::forgetSmsDataFromSession();
return redirect()->back()->withErrors($validator);
}
namespace App\Models;
class MySmsModel extends Toplan\Sms\Sms {
//override
public function send()
{
//发送入口
}
//override
public function sendProcess()
{
//发送过程
}
//more functions...
}
'smsModel' => 'App\Models\MySmsModel',
'Foo' => [
'verifySmsTemplateId' => '',
'isResendFailedSmsInQueue' => false,
'xxx' => 'some info',
...
]
namespace Toplan\Sms;
class FooAgent extends Agent {
//override
//发送短信一级入口
public function sendSms($tempId, $to, Array $data, $content){
//在这个方法中调用二级入口
//根据你使用的服务商的接口选择调用哪个方式发送短信
$this->sendContentSms($to, $content);
$this->sendTemplateSms($tempId, $to, Array $data);
}
//override
//发送短信二级入口:发送内容短信
public function sendContentSms($to, $content)
{
//通过$this->config['key'],获取配置文件中的参数
$x = $this->config['xxx'];
$x = $this->xxx;//也可以这样获取配置参数
//在这里实现发送内容短信,即直接发送内容
...
//切记将发送结果存入到$this->result
$this->result['success'] = false;//是否发送成功
$this->result['info'] = $this->currentAgentName . ':' . '发送结果说明';//发送结果信息说明
$this->result['code'] = $code;//发送结果代码
}
//override
//发送短信二级入口:发送模板短信
public function sendTemplateSms($tempId, $to, Array $data)
{
//同上...
}
//override
//发送语音验证码
public function voiceVerify($to, $code)
{
//同上...
//与发送短信唯一不同的是,切记返回结果数组
return $this->result;
}
}