PHP code example of smilestt / laravel-sms

1. Go to this page and download the library: Download smilestt/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/ */

    

smilestt / laravel-sms example snippets


smilestt\Sms\SmsServiceProvider::class,

'Sms' => smilestt\Sms\Facades\Sms::class,

php artisan vendor:publish --tag=smsconfig

//config/sms.php
//存储器
'storage' => [
        'prefix' => '',//存储key的前缀
        'driver' => 'cache',//存储方式,内置可选的值有'session'和'cache',api路由中请使用cache
]



namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Sms;

class HomeController extends Controller
{
    public function index(){
    	$gateway = Sms::gateway('qcloud');
    	//发送
    	echo $gateway->send('测试手机号码');
    	//校验码是否正确
    	echo $gateway->verifyCode('测试手机号码','验证码');
    }
}

Validator::extend('verify_sms_code', function ($attribute, $value, $parameters) {
	$mobile = app('request')->input($parameters[0]);
	$gateway = \Sms::gateway(config('sms.default'));
	return $gateway->verifyCode($mobile,$value);
});

$validator = Validator::make($data, [
      'phone' => 'unique:表名',
      'verifyCode' => 'verify_sms_code:phone',//phone为表单中的手机号字段名
]);

vendor\bin\phpunit tests\SmsUnitTest.php