生活随笔
收集整理的這篇文章主要介紹了
thinkphp5.1使用云之讯
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
下載云之訊SDK
http://docs.ucpaas.com/doku.php?id=sms_sdk解壓
只要lib下的Ucpaas.class.php文件,修改名字為Ucpaas.php,放到如下位置:
添加命名空間,其他的不用修改
namespace sms;class Ucpaas
{const BaseUrl
= "https://open.ucpaas.com/ol/sms/";private $accountSid;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;}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;}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;}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;}
}
封裝發短信的類庫,在app/common/lib/sms下新建Sms.php
Sms.php代碼如下
namespace app\common\lib\sms;use sms\Ucpaas;class Sms
{private $ucpaas;public function __construct(){$options['accountsid'] = config('sms.accountSid');$options['token'] = config('sms.token');$this->ucpaas = new Ucpaas($options);}public function smsSend($phone, $code){$appid = config('sms.appId'); $templateid = config('sms.templateId'); $param = $code; $mobile = $phone;$uid = "";$res = $this->ucpaas->SendSms($appid,$templateid,$param,$mobile,$uid);return $res;}
}
在app/index/controller/index.php試用自己封裝的類庫
namespace app\index\controller;use app\common\lib\sms\Sms;
use think\Controller;class Index extends Controller
{public function index(){$sms = new Sms();$phone = '';$code = self
::getCode();$data = $sms->smsSend($phone, $code);halt($data);}public static function getCode($num = 4){if ($num == 4) {return rand(1000, 9999);} else {return rand(100000, 999999);}}
}
訪問 http://<自己的域名>,得到如下結果
總結
以上是生活随笔為你收集整理的thinkphp5.1使用云之讯的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。