2025-04-10 23:19:13 +08:00

132 lines
4.6 KiB
PHP
Executable File
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?php
/**
* @Author: fm453
* @Date: 2018-06-04 18:27:26
* @Last Modified by: fm453
* @Last Modified time: 2019-10-07 13:19:43
* @Email: fm453@lukegzs.com
*/
namespace vendor\aliyun\dysms;
require_once (__DIR__) . "/DySdkLite/SignatureHelper.php";
use Aliyun\DySDKLite\SignatureHelper as Helper;
class Sms
{
const BEFOR_SEND_SMS = "befor_send_sms_event";
const AFTER_SEND_SMS_SUCCESS = "after_send_sms_event_success";
const AFTER_SEND_SMS_ERROR = "after_send_sms_event_error";
/**
* @var object sdk接口对象
*/
public $errors = "";
public $OutId = "12345";
public $SmsUpExtendCode = "1234567";
private $_grepMobile = "/^1[3,4,5,6,7,8,9]\d{9}$/";
private $regionid;
private $accessKeyId;
private $accessKeySecret;
private $sign;
/**
* Sms constructor.
* @param string $sdkName 接口名称
* @param array $option 接口的配置参数
* @param array $config
*/
public function __construct($option=[], array $config = [])
{
$option['regionid'] = isset($option['regionid'])? $option['regionid']:'cn-hangzhou';
if (!isset($option['regionid']) || empty($option['regionid'])) {
$this->errors = '参数缺失regionid必须要设置';
return false;
}
if (!isset($config['accessKeyId']) || empty($config['accessKeyId'])) {
$this->errors = '参数缺失accessKeyId必须要设置';
return false;
}
if (!isset($config['accessKeySecret']) || empty($config['accessKeySecret'])) {
$this->errors = '参数缺失accessKeySecret必须要设置';
return false;
}
if (!isset($config['sign']) || empty($config['sign'])) {
$this->errors = '参数缺失短信签名sign必须要设置';
return false;
}
$this->regionid = $option['regionid'];
$this->accessKeyId = $config['accessKeyId'];
$this->accessKeySecret = $config['accessKeySecret'];
$this->sign = $config['sign'];
if (isset($option['OutId']) || !empty($config['OutId'])) {
$this->OutId = $option['OutId'];
}
if (isset($option['SmsUpExtendCode']) || !empty($config['SmsUpExtendCode'])) {
$this->SmsUpExtendCode = $option['SmsUpExtendCode'];
}
}
public function send($phone,$tmpl,$content=[]){
$params = array ();
// *** 需用户填写部分 ***
// fixme 必填: 请参阅 https://ak-console.aliyun.com/ 取得您的AK信息
$accessKeyId = $this->accessKeyId;
$accessKeySecret = $this->accessKeySecret;
// fixme 必填: 短信接收号码
if (empty($phone)) {
$this->errors = '电话号码phone必须要填写';
return false;
}
$params["PhoneNumbers"] = $phone;
// fixme 必填: 短信签名,应严格按"签名名称"填写,请参考: https://dysms.console.aliyun.com/dysms.htm#/develop/sign
$params["SignName"] = $this->sign;
// fixme 必填: 短信模板Code应严格按"模板CODE"填写, 请参考: https://dysms.console.aliyun.com/dysms.htm#/develop/template
if (empty($tmpl)) {
$this->errors = '短信模板编码必须填写';
return false;
}
$params["TemplateCode"] = $tmpl;
// fixme 可选: 设置模板参数, 假如模板中存在变量需要替换则为必填项
$params['TemplateParam'] = $content;
// fixme 可选: 设置发送短信流水号
$params['OutId'] = $this->OutId;
// fixme 可选: 上行短信扩展码, 扩展码字段控制在7位或以下无特殊需求用户请忽略此字段
$params['SmsUpExtendCode'] = $this->SmsUpExtendCode;
// *** 需用户填写部分结束, 以下代码若无必要无需更改 ***
if(!empty($params["TemplateParam"]) && is_array($params["TemplateParam"])) {
$params["TemplateParam"] = json_encode($params["TemplateParam"], JSON_UNESCAPED_UNICODE);
}
// 初始化SignatureHelper实例用于设置参数签名以及发送请求
$helper = new Helper();
// 此处可能会抛出异常注意catch
$content = $helper->request(
$accessKeyId,
$accessKeySecret,
"dysmsapi.aliyuncs.com",
array_merge($params, array(
"RegionId" => "cn-hangzhou",
"Action" => "SendSms",
"Version" => "2017-05-25",
))
// fixme 选填: 启用https
// ,true
);
return $content;
}
}