PHP code example of singka / singka-sms
1. Go to this page and download the library: Download singka/singka-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/ */
singka / singka-sms example snippets
// +----------------------------------------------------------------------
// | 胜家云 [ SingKa Cloud ]
// +----------------------------------------------------------------------
// | Copyright (c) 2016~2020 https://www.singka.net All rights reserved.
// +----------------------------------------------------------------------
// | 宁波晟嘉网络科技有限公司
// +----------------------------------------------------------------------
// | Author: ShyComet <[email protected] >
// +----------------------------------------------------------------------
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',
],
],
]
];
// +----------------------------------------------------------------------
// | 胜家云 [ SingKa Cloud ]
// +----------------------------------------------------------------------
// | Copyright (c) 2016~2020 https://www.singka.net All rights reserved.
// +----------------------------------------------------------------------
// | 宁波晟嘉网络科技有限公司
// +----------------------------------------------------------------------
// | Author: ShyComet <[email protected] >
// +----------------------------------------------------------------------
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;
}
}