阿里云最新的,轻量版的,php5.3 以上,整合thinkphp3.2
Alisms类
<?php
namespace Common\Controller;
use Think\Controller;
use Aliyun\DySDKLite\SignatureHelper;
require_once "./Application/Class/Alisms/SignatureHelper.php"; //SignatureHelper.php 的路径你们随意哈,文件去阿里去官方下载,或者本文附件里
class AlismsController extends Controller {
public function _initialize(){
$this->accessKeyId = "LTAI123123e24GIc"; //keyid
$this->accessKeySecret = "6SBiPi1u123213UpPGW"; //keysecret
$this->SignName = "梦雪"; //签名
$this->CodeId = "SMS_11902313131"; //验证码模板id
}
//发送验证码
public function code($phone,&$msg){
if(!$this->isphone($phone)){
$msg = "手机号不正确";
return false;
}
$params["PhoneNumbers"] = $phone;
$params["TemplateCode"] = $this->CodeId; //模板
//记录验证码
if(session("?code")){
$code = session("code");
}else{
$code = rand(100000,999999);
session("code",$code);
}
$params['TemplateParam'] = ["code" => $code]; //验证码
return $this->send($params,$msg);
}
private function isphone($phone){
if (!is_numeric($phone)) {
return false;
}
return preg_match('#^13[\d]{9}$|^14[5,7]{1}\d{8}$|^15[^4]{1}\d{8}$|^17[0,6,7,8]{1}\d{8}$|^18[\d]{9}$#', $phone) ? true : false;
}
//发送
private function send($params=[],&$msg){
$params["SignName"] = $this->SignName;
if(!empty($params["TemplateParam"]) && is_array($params["TemplateParam"])) {
$params["TemplateParam"] = json_encode($params["TemplateParam"], JSON_UNESCAPED_UNICODE);
}
$helper = new SignatureHelper();
$content = $helper->request(
$this->accessKeyId,
$this->accessKeySecret,
"dysmsapi.aliyuncs.com",
array_merge($params, array(
"RegionId" => "cn-hangzhou",
"Action" => "SendSms",
"Version" => "2017-05-25",
))
);
if($content===false){
$msg = "发送异常";
return false;
}else{
$data = (array)$content;
if($data['Code']=="OK"){
$msg = "发送成功";
return true;
}else{
$msg = "发送失败 ".$data['Message'];
return false;
}
}
}
}如何使用
<?php
namespace Home\Controller;
use Think\Controller;
use Common\Controller\AlismsController;
class LoginController extends Controller {
public function code(){
$code = new AlismsController(); //这个类我放在了Common\Controller\下面,你们随意哈
$code->code("15912345678",$msg);
echo $msg;
}
}
//更多好用代码找我们交流
//开发遇到问题,找以下QQ群,众多高手免费解答
//QQ群 324098841