记一下PHP云人才招聘系统对接阿里云短信的方法
# SDK下载
下载官方SDK (opens new window)之后将api_sdk拷贝到系统的app/public目录下,当然也可放在项目内其它位置
# 功能实现
在app/public/common.php使用类库
require_once '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;
增加公用函数
function send_sms_code($data)
{
Config::load();
$accessKeyId = "xxx";//阿里云短信keyId
$accessKeySecret = "xxx";//阿里云短信keysecret
//短信API产品名
$product = "Dysmsapi";
//短信API产品域名
$domain = "dysmsapi.aliyuncs.com";
//暂时不支持多Region
$region = "cn-hangzhou";
//初始化访问的acsCleint
$profile = DefaultProfile::getProfile($region, $accessKeyId, $accessKeySecret);
DefaultProfile::addEndpoint("cn-hangzhou", "cn-hangzhou", $product, $domain);
$acsClient= new DefaultAcsClient($profile);
$request = new SendSmsRequest();
$request->setPhoneNumbers($data['mobile']);//必填-短信接收号码
$request->setSignName('xxxx');//必填-短信签名: 如何添加签名可以参考阿里云短信或TPshop官方文档
//必填-短信模板Code
$request->setTemplateCode($data['tpl_code']);//必填-短信签名: 如何添加签名可以参考阿里云短信或TPshop官方文档
//选填-假如模板中存在变量需要替换则为必填(JSON格式)
$code = $data['code'];
$request->setTemplateParam("{\"code\":$code}");//短信签名内容:
//发起访问请求
$resp = $acsClient->getAcsResponse($request);
//短信发送成功返回True,失败返回false
if ($resp && $resp->Code == 'OK') {
return array('status' => 1, 'msg' => $resp->Code);
} else {
return array('status' => -1, 'msg' => $resp->Message . ' subcode:' . $resp->Code);
}
}
# 调用
调用函数:send_sms_code($data)
$data['mobile']='xxxxxx';
$data['tpl_code']='xxx';
$data['code']='xxxxxx';
$result = $this->send_sms_code($data);
if($res['status'] != -1){
return $this->response(200,'发送成功!');
}else{
return $this->response(400,'发送失败!');
}