美文网首页Laravel
Larval-sms + 阿里大鱼 配置定时发送短信服务

Larval-sms + 阿里大鱼 配置定时发送短信服务

作者: luozijian | 来源:发表于2017-05-31 00:13 被阅读0次
file

安装

  1. 引入依赖

    composer require toplan/laravel-sms:~2.6
    
  2. 参数配置

    • 在config/app.php文件中providers数组里加入:

      Toplan\PhpSms\PhpSmsServiceProvider::class,
      Toplan\Sms\SmsManagerServiceProvider::class,
      
  • 在config/app.php文件中的aliases数组里加入:

    'PhpSms' => Toplan\PhpSms\Facades\Sms::class,
    'SmsManager' => Toplan\Sms\Facades\SmsManager::class,
    

配置

  1. 修改 config/phpsms.php

    scheme' => [
     'Alidayu',//配置代理器为阿里大鱼
    ],
    
  2. 修改 app/helpers.php ,复制以下内容进去

    function sendMessage($mobile,$template_id,$tempData){
        $templates = [
            'Alidayu' => $template_id//模板id
        ];
        Toplan\PhpSms\Sms::make()->to($mobile)->template($templates)->data($tempData)->send();
    }
    

Artisan 命令行

  1. 生成命令

    php artisan make:command SendMessage
    
  2. 配置命令

    刚才生成的 Artisan 命令行 文件在 app/Console/Commands 目录,我们修改目录下刚刚生成的的 SendMessage.php 文件

    <?php
    
    namespace App\Console\Commands;
    
    use Illuminate\Console\Command;
    
    class SendMessage extends Command
    {
        /**
         * The name and signature of the console command.
         *
         * @var string
         */
        protected $signature = 'message:send';//命令的格式,比如这里命令就是 php artisan message:send
    
        /**
         * The console command description.
         *
         * @var string
         */
        protected $description = 'Send message to user';//命令的描述
    
        /**
         * Create a new command instance.
         *
         * @return void
         */
        public function __construct()
        {
            parent::__construct();
        }
    
        /**
         * Execute the console command.
         *
         * @return mixed
         */
        public function handle()
        {
            //这里处理你的业务逻辑
            $mobile = 11111111111;//要发送的手机号
            template_id = 111111;//你的阿里大鱼模板id
            $tempData = [
                 'name' => '123',//模板变量自定
         ];
            sendMessage($mobile,$template_id,$tempData);
        }
    }
    
  3. 注册命令

    修改 app/Console/Kernel.php 文件,添加以下内容

    protected $commands = [
     Commands\SendMessage::class,
    ];
    
  4. 确认命令正确生成和配置

    使用 php artisan list 命令,如果能看到 php artisan message:send 说明配置成功

定时任务

在你的服务器(linux)使用 crontab -e,添加以下内容

* * 1 * * cd 你的项目根目录 && php artisan message:send

这是每天执行一次的定时任务,如果你还不明白定时任务怎么使用,请查看 定时任务用法例子

Debug

为了方便我们排查错误,我们在数据库创建一个日志表 laravel-sms

  1. 创建 migration 文件

    php artisan make:migration create_sms_table --create
    
  2. 复制以下内容进去

    <?php
    
    use Illuminate\Database\Migrations\Migration;
    use Illuminate\Database\Schema\Blueprint;
    
    class CreateSmsTable extends Migration
    {
        /**
         * Run the migrations.
         *
         * @return void
         */
        public function up()
        {
            Schema::create('laravel_sms', function (Blueprint $table) {
                $table->increments('id');
    
                //to:用于存储手机号
                $table->string('to')->default('');
    
                //temp_id:存储模板标记,用于存储任何第三方服务商提供的短信模板标记/id
                $table->string('temp_id')->default('');
    
                //data:模板短信的模板数据,建议json格式
                $table->string('data')->default('');
    
                //content:内容
                $table->string('content')->default('');
    
                //voice_code:语言验证码code
                $table->string('voice_code')->default('');
    
                //发送失败次数
                $table->mediumInteger('fail_times')->default(0);
    
                //最后一次发送失败时间
                $table->integer('last_fail_time')->unsigned()->default(0);
    
                //发送成功时的时间
                $table->integer('sent_time')->unsigned()->default(0);
    
                //代理器使用日志,记录每个代理器的发送状态,可用于排错
                $table->text('result_info')->nullable();
    
                $table->timestamps();
                $table->softDeletes();
                $table->engine = 'InnoDB';
    
                //说明
                //1:temp_id和data用于发送模板短信。
                //2:content用于直接发送短信内容,不使用模板。
                //3:voice_code用于存储语言验证码code。
            });
        }
    
        /**
         * Reverse the migrations.
         *
         * @return void
         */
        public function down()
        {
            Schema::dropIfExists('laravel_sms');
        }
    }
    
  3. 迁移数据库

    php artisan migrate

    这样我们就可以很方便的查看日志来排查错误啦

其他

laravel-sms 官方文档:https://github.com/toplan/laravel-sms
php-sms 官方文档:https://github.com/toplan/phpsms
Artisan 命令行官方文档:http://d.laravel-china.org/docs/5.3/artisan#registering-commands

相关文章

网友评论

    本文标题:Larval-sms + 阿里大鱼 配置定时发送短信服务

    本文链接:https://www.haomeiwen.com/subject/vvrdfxtx.html