在工具箱下面添加SmsSDK.class.php文件,下面内容直接复制粘贴,配置自己的信息
<?php
namespace Vendor;
class SmsSDK{
function __construct($Address,$Port,$Account,$Token){
$this->Address = "";//IP地址 不加http://
$this->Port = "";//端口
$this->Account = "";//账户
$this->Token = "";//token
}
/**
* 发起HTTP请求
*/
function curl_post($url,$data,$header,$post=1)
{
//初始化curl
$ch = curl_init();
//参数设置
$res= curl_setopt($ch, CURLOPT_URL,$url);//请求地址
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);//不验证host
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);//不验证证书
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_POST, $post);//请求方式
if($post){
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);//post数据
}
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch,CURLOPT_HTTPHEADER,$header);//设置头信息
$result = curl_exec ($ch);
//连接失败
if($result == FALSE){
$result = "";
}
curl_close($ch);
return $result;
}
/**
* 发送短信(单条)
* @param tempId 模板Id
* @param mobile 手机号码
* @param body 内容
* @param rType 响应数据类型 0 Json 类型,1 xml 类型
*/
function send($tempId, $mobile, $body, $rType)
{
// 生成请求
//$body = urlencode(mb_convert_encoding($body, 'utf-8', 'gb2312'));//url编码
$url = "http://$this->Address:$this->Port/SMS/Send";
$data = "account=$this->Account&token=$this->Token&tempid=$tempId&mobile=$mobile&content=$body&type=$rType";
// 生成包头
$header = array("charset=utf-8");
// 发送请求
return $this->curl_post($url,$data,$header,1);
}
/**
* 发送短信(群发)
* @param tempId 模板Id
* @param mobile 手机号码,多个号码用英文分号;分割
* @param body 短信内容
* @param rType 响应数据类型 0 Json 类型,1 xml 类型
*/
function sendMultiple($tempId, $mobile, $body, $rType)
{
// 生成请求
$body = urlencode(mb_convert_encoding($body, 'utf-8', 'gb2312'));//url编码
$url = "http://$this->Address:$this->Port/SMS/SendMultiple";
$data = "account=$this->Account&token=$this->Token&tempid=$tempId&mobile=$mobile&content=$body&type=$rType";
// 生成包头
$header = array("charset=utf-8");
// 发送请求
return $this->curl_post($url,$data,$header,1);
}
/**
* 获取短信状态
* @param smsId 短信Id
* @param rType 响应数据类型 0 Json 类型,1 xml 类型
*/
function smsStatus($smsId, $rType)
{
// 生成请求
$url = "http://$this->Address:$this->Port/SMS/SMSStatus";
$data = "account=$this->Account&token=$this->Token&smsid=$smsId&type=$rType";
// 生成包头
$header = array("charset=utf-8");
// 发送请求
return $this->curl_post($url,$data,$header,1);
}
/**
* 获取剩余可发短信条数
* @param rType 响应数据类型 0 Json 类型,1 xml 类型
*/
function smsNum($rType)
{
// 生成请求
$url = "http://$this->Address:$this->Port/SMS/SMSNum";
$data = "account=$this->Account&token=$this->Token&type=$rType";
// 生成包头
$header = array("charset=utf-8");
// 发送请求
return $this->curl_post($url,$data,$header,1);
}
function sendSMS($tempId,$mobile,$body,$rType,$mToken,$extno)
{
global $Address,$Port,$Account,$Token;
//初始化SDK
$rest = new KXTSmsSDK($Address,$Port,$Account,$Token);
// 发送短信
$result = $rest->send($tempId, $mobile, $body, $rType,$mToken,$extno);
if($result == NULL ) {
//echo "result error!";
break;
}
//自己代码业务逻辑
//echo $result;
}
}
/**
* 发送短信(单条)
* 模板Id
* 发送合法的手机号
* 短信内容
* 响应类型 0 json类型,1 xml类型
* $mToken = "";//可选
* $extno ="";//扩展号 可选
*/
?>
在对应的控制器里面引用SmsSDK.class.php
在命名空间下面加入use Vendor;
在触发短信的事件加入下面的代码
$SMS = new \Vendor\KXTSmsSDK;//实例化
$SMS->sendSMS("模板Id","模板内容",1,"","");//需对应短信模板列表
本文详细介绍了如何在工具箱中集成并使用SmsSDK.class.php实现短信发送功能,包括初始化SDK、发送单条和群发短信、获取短信状态及剩余可发短信数量等操作。

1万+

被折叠的 条评论
为什么被折叠?



