
发送短信代码如下:
private function sendSmsByAliyun()
{
include_once './vendor/aliyun-php-sdk-core/Config.php';
include_once './vendor/Dysmsapi/Request/V20170525/SendSmsRequest.php';
$accessKeyId = $this->config['sms_appkey'];//阿里云短信keyId
$accessKeySecret = $this->config['sms_secretKey'];//阿里云短信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 DysmsapiRequestV20170525SendSmsRequest;
$request->setPhoneNumbers("138xxx");//必填-短信接收号码
$request->setSignName("xxxx");//必填-短信签名: 如何添加签名可以参考阿里云短信或TPshop官方文档
//必填-短信模板Code
$request->setTemplateCode("修改手机号码");//必填-短信签名: 如何添加签名可以参考阿里云短信或TPshop官方文档
//选填-假如模板中存在变量需要替换则为必填(JSON格式)
$request->setTemplateParam("{"code":"4894"}");//短信签名内容:
//选填-发送短信流水号
//$request->setOutId("1234");
//发起访问请求
$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);
}
}注意, 引用阿里云短信的SDK文件前要加”./”, 否则可能找不到PHP文件.
集成后, 测试效果如下


| 欢迎光临 一起源码网 (https://www.171739.xyz/) | Powered by Discuz! X3.3 |