雲之訊短信開發
阿新 • • 發佈:2018-07-18
號碼 isset font tran class ssl eth content method
一、在app\comom中建一個公共的(封裝類)
<?php /** * Created by PhpStorm. * User: Administrator * Date: 2018/7/17 0017 * Time: 下午 9:11 * 雲之訊類, * 用於發送短信 */ namespace app\common; class Ucpaas{ //API請求地址 const BaseUrl = "https://open.ucpaas.com/ol/sms/"; //開發者賬號ID。由32個英文字母和阿拉伯數字組成的開發者賬號唯一標識符。 private $accountSid; //開發者賬號TOKEN private $token; public function __construct($options) { if (is_array($options) && !empty($options)) { $this->accountSid = isset($options[‘accountsid‘]) ? $options[‘accountsid‘] : ‘‘; $this->token = isset($options[‘token‘]) ? $options[‘token‘] : ‘‘; } else { throw new Exception("非法參數"); } } private function getResult($url, $body = null, $method) { $data = $this->connection($url,$body,$method); if (isset($data) && !empty($data)) { $result = $data; } else { $result = ‘沒有返回數據‘; } return $result; } /** * @param $url 請求鏈接 * @param $body post數據 * @param $method post或get * @return mixed|string */ private function connection($url, $body,$method) { if (function_exists("curl_init")) { $header = array( ‘Accept:application/json‘, ‘Content-Type:application/json;charset=utf-8‘, ); $ch = curl_init($url); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_HTTPHEADER, $header); if($method == ‘post‘){ curl_setopt($ch,CURLOPT_POST,1); curl_setopt($ch,CURLOPT_POSTFIELDS,$body); } curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false); $result = curl_exec($ch); curl_close($ch); } else { $opts = array(); $opts[‘http‘] = array(); $headers = array( "method" => strtoupper($method), ); $headers[]= ‘Accept:application/json‘; $headers[‘header‘] = array(); $headers[‘header‘][]= ‘Content-Type:application/json;charset=utf-8‘; if(!empty($body)) { $headers[‘header‘][]= ‘Content-Length:‘.strlen($body); $headers[‘content‘]= $body; } $opts[‘http‘] = $headers; $result = file_get_contents($url, false, stream_context_create($opts)); } return $result; } /** 單條發送短信的function,適用於註冊/找回密碼/認證/操作提醒等單個用戶單條短信的發送場景 * @param $appid 應用ID * @param $mobile 接收短信的手機號碼 * @param $templateid 短信模板,可在後臺短信產品→選擇接入的應用→短信模板-模板ID,查看該模板ID * @param null $param 變量參數,多個參數使用英文逗號隔開(如:param=“a,b,c”) * @param $uid 用於貴司標識短信的參數,按需選填。 * @return mixed|string * @throws Exception */ public function SendSms($appid,$templateid,$param=null,$mobile,$uid){ $url = self::BaseUrl . ‘sendsms‘; $body_json = array( ‘sid‘=>$this->accountSid, ‘token‘=>$this->token, ‘appid‘=>$appid, ‘templateid‘=>$templateid, ‘param‘=>$param, ‘mobile‘=>$mobile, ‘uid‘=>$uid, ); $body = json_encode($body_json); $data = $this->getResult($url, $body,‘post‘); return $data; } /** 群發送短信的function,適用於運營/告警/批量通知等多用戶的發送場景 * @param $appid 應用ID * @param $mobileList 接收短信的手機號碼,多個號碼將用英文逗號隔開,如“18088888888,15055555555,13100000000” * @param $templateid 短信模板,可在後臺短信產品→選擇接入的應用→短信模板-模板ID,查看該模板ID * @param null $param 變量參數,多個參數使用英文逗號隔開(如:param=“a,b,c”) * @param $uid 用於貴司標識短信的參數,按需選填。 * @return mixed|string * @throws Exception */ public function SendSms_Batch($appid,$templateid,$param=null,$mobileList,$uid){ $url = self::BaseUrl . ‘sendsms_batch‘; $body_json = array( ‘sid‘=>$this->accountSid, ‘token‘=>$this->token, ‘appid‘=>$appid, ‘templateid‘=>$templateid, ‘param‘=>$param, ‘mobile‘=>$mobileList, ‘uid‘=>$uid, ); $body = json_encode($body_json); $data = $this->getResult($url, $body,‘post‘); return $data; } }
二、在模塊中的model中建一個類(應用類)
<?php /** * Created by PhpStorm. * User: Administrator * Date: 2018/7/17 0017 * Time: 下午 9:17 * 短信實現類 */ namespace app\index\model; use think\Model; use app\common\Ucpaas; class ServerSid extends Model { private static $options = array(); public static $ucpass = null; public static $appid = null; public static $templateid = null; public function __construct() { //初始化必填 //填寫在開發者控制臺首頁上的Account Sid self::$options[‘accountsid‘] = ‘0a95667b776d7bbxxxxxxxxxxxxxx‘; //填寫在開發者控制臺首頁上的Auth Token self::$options[‘token‘] = ‘de5c9fb5f0xxxxxxxxxxxxx‘; //應用的ID,可在開發者控制臺內的短信產品下查看 self::$appid = "76a98d7fxxxxxxxxxxxxxxxxxxxxxxx"; //可在後臺短信產品→選擇接入的應用→短信模板-模板ID,查看該模板ID self::$ucpass = new Ucpaas(self::$options); } /** * 單條短信發送 * @param $templateid 短信模板,可在後臺短信產品→選擇接入的應用→短信模板-模板ID,查看該模板ID * @param $param //多個參數使用英文逗號隔開(如:param=“a,b,c”),如為參數則留空 * @param $mobile //發送到的手機號碼 * @param $uid */ public function one_sendSms($templateid="351494", $param, $mobile, $uid) { self::$ucpass->SendSms(self::$appid, $templateid, $param, $mobile, $uid); } /** 群發送短信的function,適用於運營/告警/批量通知等多用戶的發送場景 * * @param $mobileList 接收短信的手機號碼,多個號碼將用英文逗號隔開,如“18088888888,15055555555,13100000000” * @param $templateid 短信模板,可在後臺短信產品→選擇接入的應用→短信模板-模板ID,查看該模板ID * @param null $param 變量參數,多個參數使用英文逗號隔開(如:param=“a,b,c”) * @param $uid 用於貴司標識短信的參數,按需選填。 * @return mixed|string * @throws Exception */ public function array_Batch($templateid,$param=null,$mobileList,$uid){ $url = self::BaseUrl . ‘sendsms_batch‘; $body_json = array( ‘sid‘=>$this->accountSid, ‘token‘=>$this->token, ‘appid‘=>self::$appid, ‘templateid‘=> $templateid, ‘param‘=>$param, ‘mobile‘=>$mobileList, ‘uid‘=>$uid, ); $body = json_encode($body_json); $data = $this->getResult($url, $body,‘post‘); return $data; } }
三、調用應用類中的方法:
public function text(){ $templateid=351494; //短信模板,可在後臺短信產品→選擇接入的應用→短信模板-模板ID,查看該模板ID $param = "351494"; //多個參數使用英文逗號隔開(如:param=“a,b,c”),如為參數則留空 $mobile = 15600000000; $uid = ""; $server=new ServerSid(); $server->one_sendSms($param,$mobile,$uid); echo"到了"; exit; }
雲之訊短信開發