1.短信验证码平台
云之讯短信验证码平台 注册账号实名验证,余额充值,获取对接密钥等信息 添加短信模板并审核通过
2.发送验证码代码
<?php
namespace Home\Controller;
use Think\Controller;
class SmsController extends Controller {
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;
}
}
3.调用方法发送验证码
public function sendMessage(){
$mobile = '13912345678';
$code = '123456';
$uid = '123456';
$options['accountsid']='16997d8eeb6******530d82cb28b21';
$options['token']='73d5d709dd31******1d3dc04fe39e8';
$ucpass = new \Home\Controller\SmsController($options);
$appid = "e9d37263d4******540d26455e31e";
$templateid = "5**75";
$res = json_decode( $ucpass -> SendSms($appid,$templateid,$code ,$mobile,$uid));
dump($res);
}
转载请注明原文地址:https://ipadbbs.8miu.com/read-28635.html