PHP code example of hinet / laravel-sms

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

    

hinet / laravel-sms example snippets


Hinet\Sms\SmsServiceProvider::class,

'Sms' => Hinet\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('aliyun');
    	//发送
    	echo $gateway->send('测试手机号码');
    	//校验码是否正确
    	echo $gateway->verifyCode('测试手机号码','验证码');
    }
}

//use Illuminate\Support\Facades\Validator;
Validator::extend('verify_sms', 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:phone',//verifyCode为表单中的验证码名称,verify_sms为短信验证方法名,phone为表单中的手机号字段名
]);

vendor\bin\phpunit tests\SmsUnitTest.php