PHP通过移动云MAS发送模板短信
官方文档好多都不准确,有问题问客服!有问题问客服!有问题问客服!重要的事情说三遍!!!客服可以解决大部分问题
1.封装Mas Class
<?php
namespace common\sms;
class Mas
{
private $ecName = '***';
private $apId = '***';
private $secretKey = '***';
private $sign = '***';
private $addSerial = '';
private $norSmsUrl = 'https://112.35.10.201:28888/sms/tmpsubmit';
private function makeMacString($mobiles, $params, $templateId)
{
$macstr = $this->ecName . $this->apId . $this->secretKey . $templateId . $mobiles . $params . $this->sign . $this->addSerial;
return strtolower(md5($macstr));
}
private function post($url, $data)
{
if (!$url) {
return false;
}
$curl = curl_init();
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_TIMEOUT, 500);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
$res = curl_exec($curl);
curl_close($curl);
return $res;
}
public function sendTplSms($phoneList, $params, $templateId)
{
$mobiles = implode(',', $phoneList);
$mobiles = ltrim(rtrim($mobiles, ','),',');
$macstr = $this->makeMacString($mobiles, '["' . $params . '"]', $templateId);
$data = [
'ecName' => $this->ecName,
'apId' => $this->apId,
'secretKey' => $this->secretKey,
'templateId' => $templateId,
'mobiles' => $mobiles,
'params' => [(string) $params],
'sign'=> $this->sign,
'mac' => $macstr,
'addSerial' => $this->addSerial
];
$dataContent = base64_encode(json_encode($data));
$res = json_decode($this->post($this->norSmsUrl, $dataContent));
return $res;
}
}
2.调用封装好的类和方法
$sms_info = new Mas();
$mobile = [***,***];
$captcha_code = "123456";
$templateId = "***";
$object = $sms_info->sendTplSms($mobile, $captcha_code, $templateId);
$result = json_decode(json_encode($object),true);
if($result["success"]){
return ["code" =>0, "message" => "验证码发送成功,5分钟后过期!", "captcha" => $captcha_code];
}else{
return ["code" =>1, "message" => "验证码发送失败!"];
}
最后附上一张模板短信mac格式图
|