安裝
-
引入依賴
composer require toplan/laravel-sms:~2.6
-
參數(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,
配置
-
修改
config/phpsms.php
scheme' => [ 'Alidayu',//配置代理器為阿里大魚 ],
-
修改
app/helpers.php
箫津,復制以下內(nèi)容進去function sendMessage($mobile,$template_id,$tempData){ $templates = [ 'Alidayu' => $template_id//模板id ]; Toplan\PhpSms\Sms::make()->to($mobile)->template($templates)->data($tempData)->send(); }
Artisan 命令行
-
生成命令
php artisan make:command SendMessage
-
配置命令
剛才生成的 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è)務邏輯 $mobile = 11111111111;//要發(fā)送的手機號 template_id = 111111;//你的阿里大魚模板id $tempData = [ 'name' => '123',//模板變量自定 ]; sendMessage($mobile,$template_id,$tempData); } }
-
注冊命令
修改
app/Console/Kernel.php
文件漓柑,添加以下內(nèi)容protected $commands = [ Commands\SendMessage::class, ];
-
確認命令正確生成和配置
使用
php artisan list
命令,如果能看到php artisan message:send
說明配置成功
定時任務
在你的服務器(linux)使用 crontab -e
栋豫,添加以下內(nèi)容
* * 1 * * cd 你的項目根目錄 && php artisan message:send
這是每天執(zhí)行一次的定時任務丧鸯,如果你還不明白定時任務怎么使用嫩絮,請查看 定時任務用法例子
Debug
為了方便我們排查錯誤,我們在數(shù)據(jù)庫創(chuàng)建一個日志表 laravel-sms
-
創(chuàng)建
migration
文件php artisan make:migration create_sms_table --create
-
復制以下內(nèi)容進去
<?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:模板短信的模板數(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'); } }
-
遷移數(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