Thinkphp5对接阿里云短信接口,不使用官方demo
阿里云提供的最新对接api 个人感觉太过复杂不是很容易懂,所以根据官方接口和网上的资料整合一下阿里云发送短信的接口。
/**
* @xk 重新定义阿里云相关接口 api 第一个就是阿里云相关短信接口
* 包含短信的验证码发送和通知类的短信
* @actor xingkongyinzhe
* @time 2019-11-13 周三 青岛 天气 小雨转多云
* @var [type]
*/
namespace app\index\controller;
use think\Controller;
use think\Session;
use think\Db;
/**
* @xk 开始执行定义短信发送方案
*/
class Aliyun extends Controller
{
/**
* 构造公共函数
*/
function __construct(){
$this->al_id = ''; //阿里云ID
$this->al_access = ''; //阿里云密钥
$this->al_signName = ''; //阿里云配置的模板名称
$this->al_signCode = ''; //阿里云模板ID 阿里云通知短信模板
$this->al_signMsgCode = ''; //阿里云通知短信模板
$this->al_version = '2017-05-25'; //阿里云版本号
}
/**
* @xk 发送验证码 使用阿里云相关短信接口
* @param [type] $phone
* @return [type] [description]
*/
public function sendCode($phone=''){
if(!preg_match("/^1[34578]{1}\d{9}$/",$phone)){
return Json(array('code'=>0,'msg'=>'手机号格式错误'));
exit();
}
$code = rand(1111,9999);
$templateparam = [
'code'=>$code
];
$params =