<?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; }}