PHP code example of hyperf-ext / sms
1. Go to this page and download the library: Download hyperf-ext/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/ */
hyperf-ext / sms example snippets
[
'driver' => \HyperfExt\Sms\Drivers\AliyunDriver::class,
'config' => [
'access_key_id' => '',
'access_key_secret' => '',
'sign_name' => '',
],
]
[
'driver' => \HyperfExt\Sms\Drivers\BaiduCloudDriver::class,
'config' => [
'ak' => '',
'sk' => '',
'invoke_id' => '',
'domain' => '',
],
]
[
'driver' => \HyperfExt\Sms\Drivers\HuaweiCloudDriver::class,
'config' => [
'endpoint' => '', // 从管理控制台获取到的 App 接入地址
'app_key' => '',
'app_secret' => '',
'from' => [
'default' => '', // 默认签名通道号
// 'another' => '', // 其他签名通道号
],
],
]
[
'driver' => \HyperfExt\Sms\Drivers\JuheDataDriver::class,
'config' => [
'app_key' => '',
],
]
[
'driver' => \HyperfExt\Sms\Drivers\LuosimaoDriver::class,
'config' => [
'app_key' => '',
],
]
[
'driver' => \HyperfExt\Sms\Drivers\QiniuDriver::class,
'config' => [
'secret_key' => '',
'access_key' => '',
],
]
[
'driver' => \HyperfExt\Sms\Drivers\RongCloudDriver::class,
'config' => [
'app_key' => '',
'app_secret' => '',
],
]
[
'driver' => \HyperfExt\Sms\Drivers\RonglianDriver::class,
'config' => [
'app_id' => '',
'account_sid' => '',
'account_token' => '',
'is_sub_account' => false,
],
]
[
'driver' => \HyperfExt\Sms\Drivers\SendCloudDriver::class,
'config' => [
'sms_user' => '',
'sms_key' => '',
'timestamp' => false, // 是否启用时间戳
],
]
[
'driver' => \HyperfExt\Sms\Drivers\SmsBaoDriver::class,
'config' => [
'user' => '',
'password' => '',
],
]
[
'driver' => \HyperfExt\Sms\Drivers\TencentCloudDriver::class,
'config' => [
'sdk_app_id' => '',
'secret_id' => '',
'secret_key' => '',
'sign' => null, // 短信签名
'from' => [ // SenderId,中国大陆地区无需配置
'default' => '', // 默认 SenderId
// 'another' => '', // 其他 SenderId
],
],
]
[
'driver' => \HyperfExt\Sms\Drivers\TwilioDriver::class,
'config' => [
'account_sid' => '',
'token' => '',
'from' => [
'default' => '',
// 'another' => '',
],
],
]
[
'driver' => \HyperfExt\Sms\Drivers\UCloudDriver::class,
'config' => [
'private_key' => '',
'public_key' => '',
'sig_content' => '', // 短信签名
'project_id' => '', // 项目ID,子账号才需要该参数
],
]
[
'driver' => \HyperfExt\Sms\Drivers\YunpianDriver::class,
'config' => [
'api_key' => '',
'signature' => '', // 短信签名,内容中无签名时使用
],
]
[
'driver' => \HyperfExt\Sms\Drivers\YunxinDriver::class,
'config' => [
'app_key' => '',
'app_secret' => '',
'code_length' => 4, // 随机验证码长度,范围 4~10,默认为 4
'need_up' => false, // 是否需要支持短信上行
],
]
[
'default' => [
'senders' => ['aliyun', 'twillo'],
],
]
use HyperfExt\Sms\Smsable;
class VerificationCode extends Smsable
{
public $senders = ['aliyun', 'twillo'];
}
[
'aliyun' => ['cn', 'hk', 'mo', 'tw'], // 仅当手机号是大陆和港澳台地区时使用阿里云和腾讯云
'tencent_cloud' => ['cn', 'hk', 'mo', 'tw'],
'twillo', // 大陆和港澳台地区以外使用 Twillo
]
public function build(SenderInterface $sender): void
{
return $this
->content('您的验证码是 123456')
->signature('【HyperfExt】');
}
public function build(SenderInterface $sender): void
{
return $this
->template('SMS_001')
->signature('【HyperfExt】')
->with('code', '123456');
}
// 在配置文件中指定
'twillo' => [
'driver' => \HyperfExt\Sms\Drivers\TwilioDriver::class,
'config' => [
'account_sid' => '',
'token' => '',
'from' => [
'default' => '123', // 当未在短信消息类中指定时使用 `default` 的值
'another1' => '456',
'another2' => '789',
],
],
],
// 在短信消息类中使用
public function build(SenderInterface $sender): void
{
return $this
->from('another2');
}
namespace App\Controller;
use App\Sms\VerificationCode;
use Hyperf\HttpServer\Contract\RequestInterface;
use Hyperf\HttpServer\Contract\ResponseInterface;
use HyperfExt\Sms\Sms;
class VerificationController
{
public function send(RequestInterface $request): ResponseInterface
{
// 假定 $user 已经实现了 HasMobileNumber 接口
$user = $request->user();
// 发送
Sms::to($user)->send(new VerificationCode());
}
}
Sms::sender('twillo')
->to($request->user())
->send(new VerificationCode());
Sms::to($request->user())
->queue(new VerificationCode());
Sms::to($request->user())
->later(new VerificationCode(), 300); //延后 5 分钟发送
use HyperfExt\Contract\ShouldQueue;
use HyperfExt\Sms\Smsable;
class VerificationCode extends Smsable implements ShouldQueue
{
/**
* 列队名称。
*
* @var string
*/
public $queue = 'default';
}
shell script
php bin/hyperf.php vendor:publish hyperf-ext/sms
shell script
php bin/hyperf.php gen:sms VerificationCode