feat: 项目基础框架+配置+Kernel
This commit is contained in:
@@ -0,0 +1,25 @@
|
||||
<?php
|
||||
|
||||
namespace Kewail\Sms\Demo;
|
||||
|
||||
require_once "SmsSingleSender.php";
|
||||
|
||||
use Kewail\Sms\SmsSingleSender;
|
||||
|
||||
try {
|
||||
// 请根据实际 accesskey 和 secretkey 进行开发,以下只作为演示 sdk 使用
|
||||
$accesskey = "";
|
||||
$secretkey = "";
|
||||
$phoneNumber = "";
|
||||
|
||||
$singleSender = new SmsSingleSender($accesskey, $secretkey);
|
||||
|
||||
// 普通单发
|
||||
$result = $singleSender->send(0, "86", $phoneNumber , "【Kewail科技】您注册的验证码:128128有效时间30分钟。", "", "");
|
||||
$rsp = json_decode($result);
|
||||
echo $result;
|
||||
echo "<br>";
|
||||
|
||||
} catch (\Exception $e) {
|
||||
echo var_dump($e);
|
||||
}
|
||||
@@ -0,0 +1,74 @@
|
||||
<?php
|
||||
|
||||
// Works well with php5.3 and php5.6.
|
||||
|
||||
namespace App\DAO\Moducloud;
|
||||
|
||||
class SmsSenderUtil {
|
||||
function getRandom() {
|
||||
return rand(100000, 999999);
|
||||
}
|
||||
|
||||
function calculateSig($secretkey, $random, $curTime, $phoneNumbers) {
|
||||
$phoneNumbersString = $phoneNumbers[0];
|
||||
for ($i = 1; $i < count($phoneNumbers); $i++) {
|
||||
$phoneNumbersString .= ("," . $phoneNumbers[$i]);
|
||||
}
|
||||
return hash("sha256", "secretkey=".$secretkey."&random=".$random
|
||||
."&time=".$curTime."&mobile=".$phoneNumbersString);
|
||||
}
|
||||
|
||||
function calculateSigForTemplAndPhoneNumbers($secretkey, $random, $curTime, $phoneNumbers) {
|
||||
$phoneNumbersString = $phoneNumbers[0];
|
||||
for ($i = 1; $i < count($phoneNumbers); $i++) {
|
||||
$phoneNumbersString .= ("," . $phoneNumbers[$i]);
|
||||
}
|
||||
return hash("sha256", "secretkey=".$secretkey."&random=".$random
|
||||
."&time=".$curTime."&mobile=".$phoneNumbersString);
|
||||
}
|
||||
|
||||
function phoneNumbersToArray($nationCode, $phoneNumbers) {
|
||||
$i = 0;
|
||||
$tel = array();
|
||||
do {
|
||||
$telElement = new \stdClass();
|
||||
$telElement->nationcode = $nationCode;
|
||||
$telElement->mobile = $phoneNumbers[$i];
|
||||
array_push($tel, $telElement);
|
||||
} while (++$i < count($phoneNumbers));
|
||||
return $tel;
|
||||
}
|
||||
|
||||
function calculateSigForTempl($secretkey, $random, $curTime, $phoneNumber) {
|
||||
$phoneNumbers = array($phoneNumber);
|
||||
return $this->calculateSigForTemplAndPhoneNumbers($secretkey, $random, $curTime, $phoneNumbers);
|
||||
}
|
||||
|
||||
function sendCurlPost($url, $dataObj) {
|
||||
$curl = curl_init();
|
||||
curl_setopt($curl, CURLOPT_URL, $url);
|
||||
curl_setopt($curl, CURLOPT_HEADER, 0);
|
||||
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
|
||||
curl_setopt($curl, CURLOPT_POST, 1);
|
||||
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "POST");
|
||||
curl_setopt($curl, CURLOPT_POSTFIELDS, json_encode($dataObj));
|
||||
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 0);
|
||||
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0);
|
||||
curl_setopt($curl, CURLOPT_HTTPHEADER, array('Content-Type: application/json', 'Content-Length: ' . strlen(json_encode($dataObj))));
|
||||
$ret = curl_exec($curl);
|
||||
if (false == $ret) {
|
||||
// curl_exec failed
|
||||
$result = "{ \"result\":" . -2 . ",\"errmsg\":\"" . curl_error($curl) . "\"}";
|
||||
} else {
|
||||
$rsp = curl_getinfo($curl, CURLINFO_HTTP_CODE);
|
||||
if (200 != $rsp) {
|
||||
$result = "{ \"result\":" . -1 . ",\"errmsg\":\"". $rsp . " " . curl_error($curl) ."\"}";
|
||||
} else {
|
||||
$result = $ret;
|
||||
}
|
||||
}
|
||||
curl_close($curl);
|
||||
return $result;
|
||||
}
|
||||
}
|
||||
?>
|
||||
@@ -0,0 +1,302 @@
|
||||
<?php
|
||||
|
||||
// Works well with php5.3 and php5.6.
|
||||
|
||||
namespace App\DAO\Moducloud;
|
||||
|
||||
require_once('SmsSenderUtil.php');
|
||||
|
||||
class SmsSingleSender {
|
||||
var $url;
|
||||
var $accesskey;
|
||||
var $secretkey;
|
||||
var $util;
|
||||
|
||||
function __construct($accesskey, $secretkey) {
|
||||
$this->url = "https://live.kewail.com/sms/v1/sendsinglesms";
|
||||
$this->accesskey = $accesskey;
|
||||
$this->secretkey = $secretkey;
|
||||
$this->util = new SmsSenderUtil();
|
||||
}
|
||||
|
||||
/**
|
||||
* 普通单发,明确指定内容,如果有多个签名,请在内容中以【】的方式添加到信息内容中,否则系统将使用默认签名
|
||||
* @param int $type 短信类型,0 为普通短信,1 营销短信
|
||||
* @param string $nationCode 国家码,如 86 为中国
|
||||
* @param string $phoneNumber 不带国家码的手机号
|
||||
* @param string $msg 信息内容,必须与申请的模板格式一致,否则将返回错误
|
||||
* @param string $extend 扩展码,可填空串
|
||||
* @param string $ext 服务端原样返回的参数,可填空串
|
||||
* @return string json string { "result": xxxxx, "errmsg": "xxxxxx" ... },被省略的内容参见协议文档
|
||||
*/
|
||||
function send($type, $nationCode, $phoneNumber, $msg, $extend = "", $ext = "") {
|
||||
/*
|
||||
请求包体
|
||||
{
|
||||
"tel": {
|
||||
"nationcode": "86",
|
||||
"mobile": "13788888888"
|
||||
},
|
||||
"type": 0,
|
||||
"msg": "你的验证码是1234",
|
||||
"sig": "fdba654e05bc0d15796713a1a1a2318c",
|
||||
"time": 1479888540,
|
||||
"extend": "",
|
||||
"ext": ""
|
||||
}
|
||||
应答包体
|
||||
{
|
||||
"result": 0,
|
||||
"errmsg": "OK",
|
||||
"ext": "",
|
||||
"sid": "xxxxxxx",
|
||||
"fee": 1
|
||||
}
|
||||
*/
|
||||
$random = $this->util->getRandom();
|
||||
$curTime = time();
|
||||
$wholeUrl = $this->url . "?accesskey=" . $this->accesskey . "&random=" . $random;
|
||||
|
||||
// 按照协议组织 post 包体
|
||||
$data = new \stdClass();
|
||||
$tel = new \stdClass();
|
||||
$tel->nationcode = "".$nationCode;
|
||||
$tel->mobile = "".$phoneNumber;
|
||||
|
||||
$data->tel = $tel;
|
||||
$data->type = (int)$type;
|
||||
$data->msg = $msg;
|
||||
$data->sig = hash("sha256",
|
||||
"secretkey=".$this->secretkey."&random=".$random."&time=".$curTime."&mobile=".$phoneNumber, FALSE);
|
||||
$data->time = $curTime;
|
||||
$data->extend = $extend;
|
||||
$data->ext = $ext;
|
||||
return $this->util->sendCurlPost($wholeUrl, $data);
|
||||
}
|
||||
|
||||
/**
|
||||
* 指定模板单发
|
||||
* @param string $nationCode 国家码,如 86 为中国
|
||||
* @param string $phoneNumber 不带国家码的手机号
|
||||
* @param int $templId 模板 id
|
||||
* @param array $params 模板参数列表,如模板 {1}...{2}...{3},那么需要带三个参数
|
||||
* @param string $sign 签名,如果填空串,系统会使用默认签名
|
||||
* @param string $extend 扩展码,可填空串
|
||||
* @param string $ext 服务端原样返回的参数,可填空串
|
||||
* @return string json string { "result": xxxxx, "errmsg": "xxxxxx" ... },被省略的内容参见协议文档
|
||||
*/
|
||||
function sendWithParam($nationCode, $phoneNumber, $templId = 0, $params, $sign = "", $extend = "", $ext = "") {
|
||||
/*
|
||||
请求包体
|
||||
{
|
||||
"tel": {
|
||||
"nationcode": "86",
|
||||
"mobile": "13788888888"
|
||||
},
|
||||
"sign": "Kewail",
|
||||
"tpl_id": 19,
|
||||
"params": [
|
||||
"验证码",
|
||||
"1234",
|
||||
"4"
|
||||
],
|
||||
"sig": "fdba654e05bc0d15796713a1a1a2318c",
|
||||
"time": 1479888540,
|
||||
"extend": "",
|
||||
"ext": ""
|
||||
}
|
||||
应答包体
|
||||
{
|
||||
"result": 0,
|
||||
"errmsg": "OK",
|
||||
"ext": "",
|
||||
"sid": "xxxxxxx",
|
||||
"fee": 1
|
||||
}
|
||||
*/
|
||||
$random = $this->util->getRandom();
|
||||
$curTime = time();
|
||||
$wholeUrl = $this->url . "?sdkaccesskey=" . $this->accesskey . "&random=" . $random;
|
||||
|
||||
// 按照协议组织 post 包体
|
||||
$data = new \stdClass();
|
||||
$tel = new \stdClass();
|
||||
$tel->nationcode = "".$nationCode;
|
||||
$tel->mobile = "".$phoneNumber;
|
||||
|
||||
$data->tel = $tel;
|
||||
$data->sig = $this->util->calculateSigForTempl($this->secretkey, $random, $curTime, $phoneNumber);
|
||||
$data->tpl_id = $templId;
|
||||
$data->params = $params;
|
||||
$data->sign = $sign;
|
||||
$data->time = $curTime;
|
||||
$data->extend = $extend;
|
||||
$data->ext = $ext;
|
||||
return $this->util->sendCurlPost($wholeUrl, $data);
|
||||
}
|
||||
}
|
||||
|
||||
class SmsMultiSender {
|
||||
var $url;
|
||||
var $accesskey;
|
||||
var $secretkey;
|
||||
var $util;
|
||||
|
||||
function __construct($accesskey, $secretkey) {
|
||||
$this->url = "https://live.kewail.com/sms/v1/sendsinglesms";
|
||||
$this->accesskey = $accesskey;
|
||||
$this->secretkey = $secretkey;
|
||||
$this->util = new SmsSenderUtil();
|
||||
}
|
||||
|
||||
/**
|
||||
* 普通群发,明确指定内容,如果有多个签名,请在内容中以【】的方式添加到信息内容中,否则系统将使用默认签名
|
||||
* 【注意】海外短信无群发功能
|
||||
* @param int $type 短信类型,0 为普通短信,1 营销短信
|
||||
* @param string $nationCode 国家码,如 86 为中国
|
||||
* @param string $phoneNumbers 不带国家码的手机号列表
|
||||
* @param string $msg 信息内容,必须与申请的模板格式一致,否则将返回错误
|
||||
* @param string $extend 扩展码,可填空串
|
||||
* @param string $ext 服务端原样返回的参数,可填空串
|
||||
* @return string json string { "result": xxxxx, "errmsg": "xxxxxx" ... },被省略的内容参见协议文档
|
||||
*/
|
||||
function send($type, $nationCode, $phoneNumbers, $msg, $extend = "", $ext = "") {
|
||||
/*
|
||||
请求包体
|
||||
{
|
||||
"tel": [
|
||||
{
|
||||
"nationcode": "86",
|
||||
"mobile": "13788888888"
|
||||
},
|
||||
{
|
||||
"nationcode": "86",
|
||||
"mobile": "13788888889"
|
||||
}
|
||||
],
|
||||
"type": 0,
|
||||
"msg": "你的验证码是1234",
|
||||
"sig": "fdba654e05bc0d15796713a1a1a2318c",
|
||||
"time": 1479888540,
|
||||
"extend": "",
|
||||
"ext": ""
|
||||
}
|
||||
应答包体
|
||||
{
|
||||
"result": 0,
|
||||
"errmsg": "OK",
|
||||
"ext": "",
|
||||
"detail": [
|
||||
{
|
||||
"result": 0,
|
||||
"errmsg": "OK",
|
||||
"mobile": "13788888888",
|
||||
"nationcode": "86",
|
||||
"sid": "xxxxxxx",
|
||||
"fee": 1
|
||||
},
|
||||
{
|
||||
"result": 0,
|
||||
"errmsg": "OK",
|
||||
"mobile": "13788888889",
|
||||
"nationcode": "86",
|
||||
"sid": "xxxxxxx",
|
||||
"fee": 1
|
||||
}
|
||||
]
|
||||
}
|
||||
*/
|
||||
$random = $this->util->getRandom();
|
||||
$curTime = time();
|
||||
$wholeUrl = $this->url . "?accesskey=" . $this->accesskey . "&random=" . $random;
|
||||
$data = new \stdClass();
|
||||
$data->tel = $this->util->phoneNumbersToArray($nationCode, $phoneNumbers);
|
||||
$data->type = $type;
|
||||
$data->msg = $msg;
|
||||
$data->sig = $this->util->calculateSig($this->secretkey, $random, $curTime, $phoneNumbers);
|
||||
$data->time = $curTime;
|
||||
$data->extend = $extend;
|
||||
$data->ext = $ext;
|
||||
return $this->util->sendCurlPost($wholeUrl, $data);
|
||||
}
|
||||
|
||||
/**
|
||||
* 指定模板群发
|
||||
* 【注意】海外短信无群发功能
|
||||
* @param string $nationCode 国家码,如 86 为中国
|
||||
* @param array $phoneNumbers 不带国家码的手机号列表
|
||||
* @param int $templId 模板 id
|
||||
* @param array $params 模板参数列表,如模板 {1}...{2}...{3},那么需要带三个参数
|
||||
* @param string $sign 签名,如果填空串,系统会使用默认签名
|
||||
* @param string $extend 扩展码,可填空串
|
||||
* @param string $ext 服务端原样返回的参数,可填空串
|
||||
* @return string json string { "result": xxxxx, "errmsg": "xxxxxx" ... },被省略的内容参见协议文档
|
||||
*/
|
||||
function sendWithParam($nationCode, $phoneNumbers, $templId, $params, $sign = "", $extend ="", $ext = "") {
|
||||
/*
|
||||
请求包体
|
||||
{
|
||||
"tel": [
|
||||
{
|
||||
"nationcode": "86",
|
||||
"mobile": "13788888888"
|
||||
},
|
||||
{
|
||||
"nationcode": "86",
|
||||
"mobile": "13788888889"
|
||||
}
|
||||
],
|
||||
"sign": "Kewail",
|
||||
"tpl_id": 19,
|
||||
"params": [
|
||||
"验证码",
|
||||
"1234",
|
||||
"4"
|
||||
],
|
||||
"sig": "fdba654e05bc0d15796713a1a1a2318c",
|
||||
"time": 1479888540,
|
||||
"extend": "",
|
||||
"ext": ""
|
||||
}
|
||||
应答包体
|
||||
{
|
||||
"result": 0,
|
||||
"errmsg": "OK",
|
||||
"ext": "",
|
||||
"detail": [
|
||||
{
|
||||
"result": 0,
|
||||
"errmsg": "OK",
|
||||
"mobile": "13788888888",
|
||||
"nationcode": "86",
|
||||
"sid": "xxxxxxx",
|
||||
"fee": 1
|
||||
},
|
||||
{
|
||||
"result": 0,
|
||||
"errmsg": "OK",
|
||||
"mobile": "13788888889",
|
||||
"nationcode": "86",
|
||||
"sid": "xxxxxxx",
|
||||
"fee": 1
|
||||
}
|
||||
]
|
||||
}
|
||||
*/
|
||||
$random = $this->util->getRandom();
|
||||
$curTime = time();
|
||||
$wholeUrl = $this->url . "?accesskey=" . $this->accesskey . "&random=" . $random;
|
||||
$data = new \stdClass();
|
||||
$data->tel = $this->util->phoneNumbersToArray($nationCode, $phoneNumbers);
|
||||
$data->sign = $sign;
|
||||
$data->tpl_id = $templId;
|
||||
$data->params = $params;
|
||||
$data->sig = $this->util->calculateSigForTemplAndPhoneNumbers(
|
||||
$this->secretkey, $random, $curTime, $phoneNumbers);
|
||||
$data->time = $curTime;
|
||||
$data->extend = $extend;
|
||||
$data->ext = $ext;
|
||||
return $this->util->sendCurlPost($wholeUrl, $data);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user