<?php
/**
* SMS短信发送服务
*/
require_once str_replace('\\','/',APPPATH) . 'libraries/api_sdk/vendor/autoload.php';
use Aliyun\Core\Config;
use Aliyun\Core\Profile\DefaultProfile;
use Aliyun\Core\DefaultAcsClient;
use Aliyun\Api\Sms\Request\V20170525\SendSmsRequest;
use Aliyun\Api\Sms\Request\V20170525\QuerySendDetailsRequest;
// 加载区域结点配置
Config::load();
class Sms extends MY_Controller {
/**
* 构造器
* @param string $accessKeyId 必填,AccessKeyId
* @param string $accessKeySecret 必填,AccessKeySecret
*/
public function __construct(){
parent::__construct();
// 短信API产品名
$product = "Dysmsapi";
// 短信API产品域名
$domain = "dysmsapi.aliyuncs.com";
// 暂时不支持多Region
$region = "cn-hangzhou";
// 服务结点
$endPointName = "cn-hangzhou";
// 初始化用户Profile实例
$this->config->load('business/send_message', true);
$profile = DefaultProfile::getProfile($region, $this->config->item('accessKeyID' , 'business/send_message'), $this->config->item('accessKeySecret' , 'business/send_message'));
// 增加服务结点
DefaultProfile::addEndpoint($endPointName, $region, $product, $domain);
// 初始化AcsClient用于发起请求
$this->acsClient = new DefaultAcsClient($profile);
}
/**
* 发送短信
*
* @param string $sign <p>
* 必填, 短信签名,应严格"签名名称"填写,参考:<a href="https://dysms.console.aliyun.com/dysms.htm#/sign">短信签名页</a>
* </p>
* @param string $code <p>
* 必填, 短信模板Code,应严格按"模板CODE"填写, 参考:<a href="https://dysms.console.aliyun.com/dysms.htm#/template">短信模板页</a>
* (e.g. SMS_0001)
* </p>
* @param string $phone 必填, 短信接收号码 (e.g. 12345678901)
* @param array|null $param
* 选填, 假如模板中存在变量需要替换则为必填项 (e.g. Array("code"=>"12345", "product"=>"阿里通信"))
* </p>
* @param string|null $outId [optional] 选填, 发送短信流水号 (e.g. 1234)
* @return stdClass
*/
public function sendSms() {
// 初始化SendSmsRequest实例用于设置发送短信的参数
$request = new SendSmsRequest();
//接受验证码的手机号码
$phone = isset($this->request['user_phone']) ? $this->request['user_phone'] : '';
if ($phone == ''){
$this->response = array( 'status_code' => 100012 , 'message' => $this->lang->line('lang100012') , 'data' => array());exit();
}
// 必填,设置雉短信接收号码
$request->setPhoneNumbers($phone);
// 必填,设置签名名称
$request->setSignName($this->config->item('sign' , 'business/send_message'));
$message_template = isset($this->request['message_template']) ? $this->request['message_template'] : 'verify_code' ;//短信模板
// 必填,设置模板CODE
$request->setTemplateCode($this->config->item('code' , 'business/send_message')[$message_template]);
// 可选,设置模板参数
$code = rand(1000,9999);
$param = array('code' => $code);
if($param){
$request->setTemplateParam(json_encode($param));
}
// 可选,设置流水号
// if($outId) {
// $request->setOutId($outId);
// }
// 发起访问请求
$acsResponse = $this->acsClient->getAcsResponse($request);
// 打印请求结果
//var_dump($acsResponse);
$add_data = array(
'sms_phone' => $phone,
'sms_val' => $code,
'sms_equipment' => $this->request['equipment'],
'sms_type_number' => $this->request['type_number'],
'sms_ip' => $this->request['ip'],
'send_time' => date('Y-m-d H:i:s',time())
);
if ($acsResponse->Code == 'OK'){
$add_data['sms_status'] = $this->config->item('sms_status_success' , 'business/send_message');
$this->response = array('status_code' => 0 , 'message' => $this->lang->line('lang0') , 'data' => (int)$code);
}else{
$add_data['sms_status'] = $this->config->item('sms_status_error' , 'business/send_message');
$this->response = array('status_code' => 100013 , 'message' => $this->lang->line('lang100013') , 'data' => array());
}
$this->load->model('system/Sms_model');
$this->Sms_model->insert($add_data);exit();
}
}