介绍
本我的项目是集成了各大云服务厂商的短信业务平台,反对 ThinkPHP5.0、ThinkPHP5.1 和 ThinkPHP6.0,由宁波晟嘉网络科技有限公司保护,目前反对阿里云、腾讯云、七牛云、又拍云、Ucloud 和华为云,您如果有其余厂商的集成需要,请通过邮件分割作者提交需要。
装置教程
应用 composer require singka/singka-sms
命令行装置即可。
装置实现后会主动生成 config/sms.php
配置文件,内容如下:
<?php
// +----------------------------------------------------------------------
// | 胜家云 [SingKa Cloud]
// +----------------------------------------------------------------------
// | Copyright (c) 2016~2020 https://www.singka.net All rights reserved.
// +----------------------------------------------------------------------
// | 宁波晟嘉网络科技有限公司
// +----------------------------------------------------------------------
// | Author: ShyComet <shycomet@qq.com>
// +----------------------------------------------------------------------
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
// +----------------------------------------------------------------------
// | 胜家云 [SingKa Cloud]
// +----------------------------------------------------------------------
// | Copyright (c) 2016~2020 https://www.singka.net All rights reserved.
// +----------------------------------------------------------------------
// | 宁波晟嘉网络科技有限公司
// +----------------------------------------------------------------------
// | Author: ShyComet <shycomet@qq.com>
// +----------------------------------------------------------------------
namespace app\home\controller;
use SingKa\Sms\SkSms;
use think\facade\Config;
class Index
{
/**
* 短信发送示例
*
* @mobile 短信发送对象手机号码
* @action 短信发送场景,会主动传入短信模板
* @parme 短信内容数组
*/
public function sendSms($mobile, $action, $parme)
{
//$this->SmsDefaultDriver 是从数据库中读取的短信默认驱动
$SmsDefaultDriver = $this->SmsDefaultDriver ?: 'aliyun';
//$this->SmsConfig 是从数据库中读取的短信配置
$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;
}
/**
* 数组主键序号化
*
* @arr 须要转换的数组
*/
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、阿里云、腾讯云、七牛云、又拍云、华为云
更多内容请拜访作者集体网站 尘墨成