介绍
本项目是集成了各大云服务厂商的短信业务平台,支持ThinkPHP5.0、ThinkPHP5.1和ThinkPHP6.0,由宁波晟嘉网络科技有限公司维护,目前支持阿里云、腾讯云、七牛云、又拍云、Ucloud和华为云,您如果有其他厂商的集成需求,请通过邮件联系作者提交需求。
安装教程
使用 composer require singka/singka-sms 命令行安装即可。
安装完成后会自动生成 config/sms.php 配置文件,内容如下:
<?php
return [
'aliyun' => [
'version' => '2017-05-25',
'host' => 'dysmsapi.aliyuncs.com',
'scheme' => 'http',
'region_id' => 'cn-hangzhou',
'access_key' => '',
'access_secret' => '',
'sign_name' => '',
'actions' => [
'register' => [
'actions_name' => '注册验证',
'template_id' => 'SMS_53115055',
],
'login' => [
'actions_name' => '登录验证',
'template_id' => 'SMS_53115057',
],
'changePassword' => [
'actions_name' => '修改密码',
'template_id' => 'SMS_53115053',
],
'changeUserinfo' => [
'actions_name' => '变更信息',
'template_id' => 'SMS_53115052',
],
],
],
'ucloud' => [
'public_key' => '',
'private_key' => '',
'project_id' => '',
'base_url' => 'https://api.ucloud.cn',
'sign_name' => '',
'actions' => [
'register' => [
'actions_name' => '注册验证',
'template_id' => 'UTA1910164E29F4',
],
'login' => [
'actions_name' => '登录验证',
'template_id' => 'UTA1910164E29F4',
],
'changePassword' => [
'actions_name' => '修改密码',
'template_id' => 'UTA1910164E29F4',
],
'changeUserinfo' => [
'actions_name' => '变更信息',
'template_id' => 'UTA1910164E29F4',
],
],
],
'qcloud' => [
'appid' => '',
'appkey' => '',
'sign_name' => '',
'actions' => [
'register' => [
'actions_name' => '注册验证',
'template_id' => '566198',
],
'login' => [
'actions_name' => '登录验证',
'template_id' => '566197',
],
'changePassword' => [
'actions_name' => '修改密码',
'template_id' => '566199',
],
'changeUserinfo' => [
'actions_name' => '变更信息',
'template_id' => '566200',
],
],
],
'qiniu' => [
'AccessKey' => '',
'SecretKey' => '',
'actions' => [
'register' => [
'actions_name' => '注册验证',
'template_id' => '1246849772845797376',
],
'login' => [
'actions_name' => '登录验证',
'template_id' => '1246849654881001472',
],
'changePassword' => [
'actions_name' => '修改密码',
'template_id' => '1246849964902977536',
],
'changeUserinfo' => [
'actions_name' => '变更信息',
'template_id' => '1246849860733243392',
],
],
],
'upyun' => [
'id' => '',
'token' => '',
'apiurl' => '',
'actions' => [
'register' => [
'actions_name' => '注册验证',
'template_id' => '2591',
],
'login' => [
'actions_name' => '登录验证',
'template_id' => '2592',
],
'changePassword' => [
'actions_name' => '修改密码',
'template_id' => '2590',
],
'changeUserinfo' => [
'actions_name' => '变更信息',
'template_id' => '2589',
],
],
],
'huawei' => [
'url' => '',
'appKey' => '',
'appSecret' => '',
'sender' => '',
'signature' => '',
'statusCallback' => '',
'actions' => [
'register' => [
'actions_name' => '注册验证',
'template_id' => '2591',
],
'login' => [
'actions_name' => '登录验证',
'template_id' => '2592',
],
'changePassword' => [
'actions_name' => '修改密码',
'template_id' => '2590',
],
'changeUserinfo' => [
'actions_name' => '变更信息',
'template_id' => '2589',
],
],
]
];
使用示例(基于ThinkPHP6.0)
<?php
namespace app\home\controller;
use SingKa\Sms\SkSms;
use think\facade\Config;
class Index
{
public function sendSms($mobile, $action, $parme)
{
$SmsDefaultDriver = $this->SmsDefaultDriver ?: 'aliyun';
$config = $this->SmsConfig ?: Config::get('sms.'.$SmsDefaultDriver);
$sms = new sksms($SmsDefaultDriver, $config);
if ($this->SmsDefaultDriver == 'aliyun') {
$result = $sms->$action($mobile, $parme);
} elseif ($this->SmsDefaultDriver == 'qiniu') {
$result = $sms->$action([$mobile], $parme);
} elseif ($this->SmsDefaultDriver == 'upyun') {
$result = $sms->$action($mobile, implode('|', $this->restoreArray($parme)));
} else {
$result = $sms->$action($mobile, $this->restoreArray($parme));
}
if ($result['code'] == 200) {
$data['code'] = 200;
$data['msg'] = '短信发送成功';
} else {
$data['code'] = $result['code'];
$data['msg'] = $result['msg'];
}
return $data;
}
public function restoreArray($arr)
{
if (!is_array($arr)){
return $arr;
}
$c = 0;
$new = [];
foreach ($arr as $key => $value) {
$new[$c] = $value;
$c++;
}
return $new;
}
}
返回的$result[‘code’]的值等于200,说明短信发送成功,否则可以根据错误码和错误提示去各个云服务查找相关信息。
其他说明
返回的相关错误码请查阅:Ucloud、阿里云、腾讯云、七牛云、又拍云、华为云
更多内容请访问作者个人网站 尘墨成
|