PHP code example of jmhc / sms
1. Go to this page and download the library: Download jmhc/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/ */
jmhc / sms example snippets
use Jmhc\Sms\Sms;
// 实现了 Jmhc\Sms\Contracts\CacheInterface 的缓存类
$cache = new xxxCache();
// 配置
$config = [
// HTTP 请求的超时时间(秒)
'timeout' => 5.0,
// 默认发送配置
'default' => [
// 网关调用策略,默认:顺序调用
'strategy' => \Overtrue\EasySms\Strategies\OrderStrategy::class,
// 默认可用的发送网关
'gateways' => [
'yunpian', 'aliyun',
],
],
// 可用的网关配置
'gateways' => [
'errorlog' => [
'file' => '/tmp/easy-sms.log',
],
'yunpian' => [
'api_key' => '824f0ff2f71cab52936axxxxxxxxxx',
],
'aliyun' => [
'access_key_id' => '',
'access_key_secret' => '',
'sign_name' => '',
],
//...
],
];
$sms = new Sms($cache, $config);
$res = $sms->setPhone(13188888888)
->setCode(6379)
->setMessage([
'content' => '您的验证码为: 6379',
])
->send();
// input
var_dump($res);
// 下次发送需要等待时间(手机号 => 等待秒数)
// [
// 13188888888 => 60,
// ]
use Jmhc\Sms\Utils\SmsCache;
// 实现了 Jmhc\Sms\Contracts\CacheInterface 的缓存类
$cache = new xxxCache();
$smsCache = new SmsCache($cache);
// 验证验证码是否正确,验证失败将会抛出 Jmhc\Sms\Exceptions\SmsException 异常
// 验证无类型的验证码
$smsCache->setPhone(13188888888)
->verify(123456);
// 验证登录类型的验证码
$smsCache->setPhone(13188888888)
->setType('login')
->verify(123456);
use Jmhc\Sms\Utils\SmsCache;
// 实现了 Jmhc\Sms\Contracts\CacheInterface 的缓存类
$cache = new xxxCache();
$smsCache = new SmsCache($cache);
// 使用无类型的验证码
$smsCache->setPhone(13188888888)
->useCode();
// 使用登录类型的验证码
$smsCache->setPhone(13188888888)
->setType('login')
->useCode();
js
// 发送网关
setGateways([
'yunpian', 'aliyun',
])
js
// 注册自定义网关
$sms->getEasySms()->extend('mygateway', function($gatewayConfig){
// $gatewayConfig 来自配置文件里的 `gateways.mygateway`
return new MyGateway($gatewayConfig);
});