亿美软通短信发送----PHP版本

xiaoxiao2021-02-28  13

<?php /** * Created by PhpStorm. * User: kung * Date: 18-1-26 * Time: 上午10:41 */ class Ymsms { private $_sms_addr = 'bjmtn.b2m.cn'; private $_send_uri = '/simpleinter/sendSMS'; private $_send_batch = '/simpleinter/sendPersonalitySMS'; private $_voice_uri = '/voice/sendSMS'; private $_appid = '****-***-****-*****'; private $_aespwd = '***********'; private $_template = '您好,您的验证码是:%s【***】'; public function __construct() { $this->CI =& get_instance(); } private function http_request($url, $data) { $data = http_build_query($data); $curl = curl_init(); curl_setopt($curl, CURLOPT_URL, $url); curl_setopt($curl, CURLOPT_POST, TRUE); curl_setopt($curl, CURLOPT_POSTFIELDS, $data); curl_setopt($curl, CURLOPT_RETURNTRANSFER, TRUE); $output = curl_exec($curl); curl_close($curl); return $output; } private function signmd5($timestamp){ return md5($this->_appid.$this->_aespwd.$timestamp); } //单条短信发送 public function send_sms($mobile,$vars) { $result = false; $content = sprintf($this->_template, $vars); $timestamp = date("YmdHis"); $sign = $this->signmd5($timestamp); $data = array( "appId" => $this->_appid, "timestamp" => $timestamp, "sign" => $sign, "mobiles" => $mobile, "content" => $content, "customSmsId" => "10001", "timerTime" => "", "extendedCode" => "1234" ); $url = $this->_sms_addr.$this->_send_uri; $resobj = $this->http_request($url, $data); $resArr = json_decode($resobj,true); if($resArr['code'] == "SUCCESS") $result = true; return $result; } //语音短信发送 public function voice_sms($mobile,$vars) { $result = false; $content = $vars; $timestamp = date("YmdHis"); $sign = $this->signmd5($timestamp); $data = array( "appId" => $this->_appid, "timestamp" => $timestamp, "sign" => $sign, "mobile" => $mobile, "content" => "$content", "customSmsId" => "10001", "timerTime" => "", "extendedCode" => "123" ); $url = $this->_sms_addr.$this->_voice_uri; $resobj = $this->http_request($url, $data); $resArr = json_decode($resobj,true); if($resArr['code'] == "SUCCESS") $result = true; return $result; } //批量发送 public function send_batch($batch_data) { $result = false; $timestamp = date("YmdHis"); $sign = $this->signmd5($timestamp); $data = array( "appId" => $this->_appid, "timestamp" => $timestamp, "sign" => $sign, "customSmsId" => "10001", "timerTime" => "", "extendedCode" => "1234" ); $msg_count = 0; foreach($batch_data as $sms){ $data[$sms['mobile']] = $sms['content']; $msg_per_count = ceil(mb_strlen($sms['content'])/68); $msg_count = $msg_count + $msg_per_count; } $this->log = []; // clear $this->log['call_method'] = __METHOD__; $this->log['batch_data'] = $batch_data; $this->log['batch_count'] = count($batch_data); $url = $this->_sms_addr.$this->_send_batch; $resobj = $this->http_request($url, $data); $this->log['response_body'] = $resobj; $resArr = json_decode($resobj,true); if($resArr['code'] == "SUCCESS"){ $result = true; $this->log['send_status'] = 'success'; $this->log['send_credit'] = count($batch_data); } else{ $this->log['send_status'] = 'failure'; } $this->save_log(); return $msg_count; } }
转载请注明原文地址: https://www.6miu.com/read-2799964.html

最新回复(0)