12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697 |
- <?php
- namespace tools;
- /**
- * 华为IoT云通信短息发送类
- */
- class Hwsms{
- private $account; //账号
- private $password; //密码
- private $host = ''; //接口域名
- private $error;
- private $sign = ''; // 短信签名
- private $templateId = ''; // 短信模板
- public function __construct($config=array())
- {
- $this->account = isset($config['account'])?$config['account']:'';
- $this->password = isset($config['password'])?$config['password']:'';
- $this->sign = isset($config['sign'])?$config['sign']:'';
- $ip = isset($config['ip'])?$config['ip']:'';
- $port = isset($config['port'])?$config['port']:'';
- $this->templateId = isset($config['templateId'])?$config['templateId']:'';
- if(!$this->account){
- $this->error = '未设置账号';
- return false;
- }
- if(!$this->password){
- $this->error = '未设置密码';
- return false;
- }
- if(!$ip){
- $this->error = '未设置IP';
- return false;
- }
- if(!$port){
- $this->error = '未设置端口';
- return false;
- }
- if(!$this->sign){
- $this->error = '未设置签名';
- return false;
- }
- if(!$this->templateId){
- $this->error = '未设置短信模板';
- return false;
- }
- $this->host = 'https://'.$ip.':'.$port;
- }
- /**
- * 发送短信
- * @param $mobiles 手机号多个以逗号隔开,最多1000个
- * @param $content 短信内容
- * @return bool true=发送成功 false=发送失败
- */
- public function send($mobiles,$templateParas,$statusCallback=''){
- if(!$mobiles){
- $this->error = '手机号错误';
- return false;
- }
- $url = $this->host.'/common/sms/sendTemplateMessage';
- //必填,全局号码格式(不包含国家码),示例:15123456789,多个号码之间用英文逗号分隔
- $mobiles = explode(',',$mobiles);//短信接收人号码
- //请求Headers
- $headers = [
- 'Content-Type: application/json;charset=UTF-8'
- ];
- $requestLists['mobiles']=$mobiles;
- $requestLists['templateId']=$this->templateId;
- $requestLists['templateParas']=$templateParas;
- $requestLists['signature']=$this->sign;
- //请求Body
- $data['account']=$this->account;
- $data['password']=$this->password;
- $data['requestLists']=array($requestLists);
- $data['statusCallback']=$statusCallback;
- $context_options = [
- 'http' => ['method' => 'POST', 'header'=> $headers, 'content' => json_encode($data, JSON_UNESCAPED_UNICODE|JSON_PRETTY_PRINT), 'ignore_errors' => true],
- 'ssl' => ['verify_peer' => false, 'verify_peer_name' => false] //为防止因HTTPS证书认证失败造成API调用失败,需要先忽略证书信任问题
- ];
- $response = file_get_contents($url, false, stream_context_create($context_options));
- tplog($response);
- return $response;
- }
- public function getError(){
- return $this->error;
- }
- }
|