标签:多个 this text headers ons get 后台 empty 认证
一、在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; }
标签:多个 this text headers ons get 后台 empty 认证
原文地址:https://www.cnblogs.com/li7671269/p/9326361.html