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

file

安裝

  1. 引入依賴(lài)

    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',//配置代理器為阿里大魚(yú)
    ],
    
  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ī)號(hào)
            template_id = 111111;//你的阿里大魚(yú)模板id
            $tempData = [
                 'name' => '123',//模板變量自定
         ];
            sendMessage($mobile,$template_id,$tempData);
        }
    }
    
  3. 注冊(cè)命令

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

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

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

定時(shí)任務(wù)

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

* * 1 * * cd 你的項(xiàng)目根目錄 && php artisan message:send

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

Debug

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

    php artisan migrate

    這樣我們就可以很方便的查看日志來(lái)排查錯(cuò)誤啦

其他

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)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

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

友情鏈接更多精彩內(nèi)容