Larval-sms + 阿里大魚 配置定時發(fā)送短信服務(wù)

file

安裝

  1. 引入依賴

    composer require toplan/laravel-sms:~2.6
    
  2. 參數(shù)配置

    • 在config/app.php文件中providers數(shù)組里加入:

      Toplan\PhpSms\PhpSmsServiceProvider::class,
      Toplan\Sms\SmsManagerServiceProvider::class,
      
  • 在config/app.php文件中的aliases數(shù)組里加入:

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

配置

  1. 修改 config/phpsms.php

    scheme' => [
     'Alidayu',//配置代理器為阿里大魚
    ],
    
  2. 修改 app/helpers.php ,復(fù)制以下內(nèi)容進(jìn)去

    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()
        {
            //這里處理你的業(yè)務(wù)邏輯
            $mobile = 11111111111;//要發(fā)送的手機(jī)號
            template_id = 111111;//你的阿里大魚模板id
            $tempData = [
                 'name' => '123',//模板變量自定
         ];
            sendMessage($mobile,$template_id,$tempData);
        }
    }
    
  3. 注冊命令

    修改 app/Console/Kernel.php 文件,添加以下內(nèi)容

    protected $commands = [
     Commands\SendMessage::class,
    ];
    
  4. 確認(rèn)命令正確生成和配置

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

定時任務(wù)

在你的服務(wù)器(linux)使用 crontab -e,添加以下內(nèi)容

* * 1 * * cd 你的項目根目錄 && php artisan message:send

這是每天執(zhí)行一次的定時任務(wù),如果你還不明白定時任務(wù)怎么使用,請查看 定時任務(wù)用法例子

Debug

為了方便我們排查錯誤,我們在數(shù)據(jù)庫創(chuàng)建一個日志表 laravel-sms

  1. 創(chuàng)建 migration 文件

    php artisan make:migration create_sms_table --create
    
  2. 復(fù)制以下內(nèi)容進(jìn)去

    <?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:用于存儲手機(jī)號
                $table->string('to')->default('');
    
                //temp_id:存儲模板標(biāo)記,用于存儲任何第三方服務(wù)商提供的短信模板標(biāo)記/id
                $table->string('temp_id')->default('');
    
                //data:模板短信的模板數(shù)據(jù),建議json格式
                $table->string('data')->default('');
    
                //content:內(nèi)容
                $table->string('content')->default('');
    
                //voice_code:語言驗證碼code
                $table->string('voice_code')->default('');
    
                //發(fā)送失敗次數(shù)
                $table->mediumInteger('fail_times')->default(0);
    
                //最后一次發(fā)送失敗時間
                $table->integer('last_fail_time')->unsigned()->default(0);
    
                //發(fā)送成功時的時間
                $table->integer('sent_time')->unsigned()->default(0);
    
                //代理器使用日志,記錄每個代理器的發(fā)送狀態(tài),可用于排錯
                $table->text('result_info')->nullable();
    
                $table->timestamps();
                $table->softDeletes();
                $table->engine = 'InnoDB';
    
                //說明
                //1:temp_id和data用于發(fā)送模板短信。
                //2:content用于直接發(fā)送短信內(nèi)容,不使用模板。
                //3:voice_code用于存儲語言驗證碼code。
            });
        }
    
        /**
         * Reverse the migrations.
         *
         * @return void
         */
        public function down()
        {
            Schema::dropIfExists('laravel_sms');
        }
    }
    
  3. 遷移數(shù)據(jù)庫

    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

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

推薦閱讀更多精彩內(nèi)容

  • 原文鏈接 必備品 文檔:Documentation API:API Reference 視頻:Laracasts ...
    layjoy閱讀 8,627評論 0 121
  • 一、配置 1.從終端或命令行進(jìn)入您的項目目錄,運(yùn)行 composer require iscms/alisms-f...
    吳濤濤閱讀 647評論 0 2
  • 文章分類 后臺文章分類列表頁模板導(dǎo)的詳細(xì)步驟建立數(shù)據(jù)表blog_category,并添加相應(yīng)的文章字段使用php ...
    JoyceZhao閱讀 1,771評論 0 14
  • 清晨路經(jīng)單位前的一條馬路。 長長的馬路里有個紅綠燈。 恰好看到車行方向亮的是綠燈,一輛公交車卻在綠燈前停了下來。正...
    迦顏閱讀 212評論 0 0
  • 總會有人熬夜陪你 下雨接你 說我愛你
    岳_42ea閱讀 222評論 0 0