首先声明不是阿里大鱼短信平台(其实阿里大鱼短信的集成反而简单些)
官方的文档是:https://help.aliyun.com/document_detail/51929.html
1. SDK下载和引入
我下载的版本是Version1.3.4,更新日期是2017-4-13,估计大家用的时候,可能都有新版本了。放在third_party下:
2. 添加短信配置文件
3. libraries添加Sms.php(名字可以自己定义)
其实也就是根据官方那个demo,添加了调用ci的短信配置文件,run方法只需要3个参数(手机号,模版,模版里的参数数组)
endPoint = $CI->config->item('aliyun_sms')['endPoint'];
$this->accessId = $CI->config->item('aliyun_sms')['accessId'];
$this->accessKey = $CI->config->item('aliyun_sms')['accessKey'];
$this->client = new Client($this->endPoint, $this->accessId, $this->accessKey);
/**
* Step 2. 获取主题引用
*/
$topicName = $CI->config->item('aliyun_sms')['topicName'];//"sms.topic-cn-hangzhou";
$topic = $this->client->getTopicRef($topicName);
/**
* Step 3. 生成SMS消息属性
*/
// 3.1 设置发送短信的签名(SMSSignName)和模板(SMSTemplateCode)
$batchSmsAttributes = new BatchSmsAttributes($CI->config->item('aliyun_sms')['SMSSignName'], $template_code);
// 3.2 (如果在短信模板中定义了参数)指定短信模板中对应参数的值
$batchSmsAttributes->addReceiver($mobile, $pars);
// $batchSmsAttributes->addReceiver("YourReceiverPhoneNumber2", array("YourSMSTemplateParamKey1" => "value1"));
$messageAttributes = new MessageAttributes(array($batchSmsAttributes));
/**
* Step 4. 设置SMS消息体(必须)
*
* 注:目前暂时不支持消息内容为空,需要指定消息内容,不为空即可。
*/
$messageBody = "smsmessage";
/**
* Step 5. 发布SMS消息
*/
$request = new PublishMessageRequest($messageBody, $messageAttributes);
try {
$res = $topic->publishMessage($request);
return $res->isSucceed();
// echo $res->isSucceed();
// echo "\n";
// echo $res->getMessageId();
// echo "\n";
} catch (MnsException $e) {
//记录错误日志,比如我的helper里面有个write_log方法
// write_log($e);
return false;
}
}
}
public function test() {
$this->load->library('Sms');
$par = array(
'number' => '123456'
);
$res = $this->sms->run('13512345678', $this->config->item('aliyun_sms')['templateCode']['vlidate_code'], $par);
echo $res;
}