thinkphp 5.0整合阿里大于验证码短信发送接口,含完整模型验证实例DEMO
为大家分享一个阿里大于短信发送接口:
首先创建一个发送模型(Send.php):
<?php
namespace app\index\model;
use think\Validate;
class Send extends \think\Model
{
public static $sms_config = [
'appkey' => '',//阿里大于APPKEY
'secretKey' => '',//阿里大于secretKey
'FreeSignName' => '安德兔',//短信签名
];
public function sms($data=[])
{
$validate = new Validate([
['param','require|array','参数必填|参数必须为数组'],
['mobile','require|/1[34578]{1}\d{9}$/','手机号错误|手机号错误'],
['template','require','模板id错误'],
]);
if (!$validate->check($data)) {
return $validate->getError();
}
define('TOP_SDK_WORK_DIR', CACHE_PATH.'sms_tmp/');
define('TOP_SDK_DEV_MODE', false);
vendor('alidayu.TopClient');
vendor('alidayu.AlibabaAliqinFcSmsNumSendRequest');
vendor('alidayu.RequestCheckUtil');
vendor('alidayu.ResultSet');
vendor('alidayu.TopLogger');
$config = self::$sms_config;
$c = new \TopClient;
$c->appkey = $config['appkey'];
$c->secretKey = $config['secretKey'];
$req = new \AlibabaAliqinFcSmsNumSendRequest;
$req->setExtend('');
$req->setSmsType('normal');
$req->setSmsFreeSignName($config['FreeSignName']);
$req->setSmsParam(json_encode($data['param']));
$req->setRecNum($data['mobile']);
$req->setSmsTemplateCode($data['template']);
$result = $c->execute($req);
$result = $this->_simplexml_to_array($result);
if(isset($result['code'])){
return $result['sub_code'];
}
return true;
}
private function _simplexml_to_array($obj)
{//该函数用于转化阿里大于返回的数据,将simplexml格式转化为数组,方面后续使用
if(count($obj) >= 1){
$result = $keys = [];
foreach($obj as $key=>$value){
isset($keys[$key]) ? ($keys[$key] += 1) : ($keys[$key] = 1);
if( $keys[$key] == 1 ){
$result[$key] = $this->_simplexml_to_array($value);
}elseif( $keys[$key] == 2 ){
$result[$key] = [$result[$key], $this->_simplexml_to_array($value)];
}else if( $keys[$key] > 2 ){
$result[$key][] = $this->_simplexml_to_array($value);
}
}
return $result;
}else if(count($obj) == 0){
return (string)$obj;
}
}
}
?>
创建好模型后,可在其他地方调用,下面我们创建一个简单的index控制器,用来测试:
<?php
namespace app\index\controller;
use app\index\model\Send;
error_reporting(0);
class Index extends \think\Controller
{
public function sms()
{
if(request()->isPost()){
$Send = new Send;
$result = $Send->sms([
'param' => ['code'=>'123456','product'=>'安德兔'],
'mobile' => input('post.mobile/s','','trim,strip_tags'),
'template' => 'SMS_12940581',
]);
if($result !== true){
return $this->error($result);
}
return $this->success('短信下发成功!');
}
return $this->fetch();
}
}
创建简单的模板文件用来测试:
<!doctype html>
<html lang="zh-CN">
<head>
<meta charset="utf-8">
<title>阿里大于短信发送测试</title>
<base href="{:request()->domain()}" />
<link href="static/css/bootstrap.css" rel="stylesheet">
<link href="static/css/common.css" rel="stylesheet">
<link href="static/css/admin.css" rel="stylesheet">
<script src="static/js/jquery-1.12.0.min.js"></script>
<script src="static/js/bootstrap.min.js"></script>
<script src="static/js/jquery.qrcode.min.js"></script>
<link rel="shortcut icon" href="favicon.ico" type="image/x-icon"/>
</head>
<body>
<div class="container">
<div class="panel panel-default">
<div class="panel-heading">
<strong>阿里大于短信发送测试</strong>
</div>
<div class="panel-body">
<form class="form-horizontal sms-form" method="post" action="{:url('index/index/sms')}">
<div class="form-group">
<label class="col-sm-2 control-label">接收手机号</label>
<div class="col-sm-10">
<input type="text" class="form-control" name="mobile" value="">
<p class="help-block">点击发送短信,您将收到:“【安德兔】验证码123456,您正在注册成为安德兔用户,感谢您的支持。”</p>
</div>
</div>
<div class="form-group">
<div class="col-sm-offset-2 col-sm-10">
<button type="submit" class="btn btn-success">发送短信</button>
</div>
</div>
</form>
</div>
<div class="panel-footer"> </div>
</div>
</div>
<script>
$(function(){
$('.sms-form').submit(function(){
var $this = $(this);
if(!$this.hasClass('lock-form')){
$this.addClass('lock-form');//锁定表单
var formData = new FormData($this[0]);
$.ajax({
url:$this.attr("action"),
type:'POST',
data:formData,
dataType:'json',
cache: false,
contentType: false,
processData: false,
success:function(s){
$this.removeClass('lock-form');//解锁表单
var html = (s.code != 1 ? '错误代码:' : '')+s.msg;
$('.panel-footer').html(html);
return false;
}
});
}
return false;
});
});
</script>
</body>
</html>
thinkphp 5.0整合阿里大于验证码短信发送接口,含完整模型验证实例DEMO的更多相关文章
- PHP阿里大于发短信教程
PHP阿里大于发短信教程 1 先去控制台 https://www.alidayu.com/center/user/account?spm=a3142.7791109.1999204004.5.ZtBQ ...
- Java版阿里云通信短信发送API接口实例(新)
阿里云通信(原名阿里大于)的短信服务(Short Message Service)是阿里云为用户提供的一种通信服务的能力,支持快速发送短信验证码.短信通知等. 完美支撑双11期间2亿用户,发送6亿短信 ...
- 短信发送接口demo
public class SendValidCode { // 短信发送的接口网关 private static String sendUrl = "******************** ...
- 短信发送接口被恶意访问的网络攻击事件(三)定位恶意IP的日志分析脚本
前言 承接前文<短信发送接口被恶意访问的网络攻击事件(二)肉搏战-阻止恶意请求>,文中有讲到一个定位非法IP的shell脚本,现在就来公布一下吧,并没有什么技术难度,只是当时花了些时间去写 ...
- 短信发送接口被恶意访问的网络攻击事件(四)完结篇--搭建WAF清理战场
前言 短信发送接口被恶意访问的网络攻击事件(一)紧张的遭遇战险胜 短信发送接口被恶意访问的网络攻击事件(二)肉搏战-阻止恶意请求 短信发送接口被恶意访问的网络攻击事件(三)定位恶意IP的日志分析脚本 ...
- Python2-Django配置阿里大于的短信验证码接口
1.短信发送开发指南地址:https://help.aliyun.com/document_detail/55491.html?spm=a2c4g.11186623.6.568.l5zTwH 2.SD ...
- 通过sails和阿里大于实现短信验证
通过sails与阿里大于来实现注册短信验证码的发送,逻辑图如下 1.用户在客户端发送手机号给服务器,服务器接收到手机号,生成对应时间戳,随机四位数验证码 2.服务器将电话号码和验证码告诉阿里大于服务器 ...
- Delphi - Delphi7 调用阿里大于实现短信消息验证
阿里大于是阿里通信旗下产品,融合了三大运营商的通信能力,提供包括短信.语音.流量直充.私密专线.店铺手机号等个性化服务.每条四分五,价钱还算公道,经老农测试,响应速度非常快,基本上是秒到.官方文档提供 ...
- 给安卓端调用的短信发送接口demo
package com.js.ai.modules.pointwall.action; import java.io.IOException; import java.util.HashMap; im ...
随机推荐
- P4412 [SHOI2004]最小生成树
传送门 不难发现,对于每一条树边肯定要减小它的权值,对于每一条非树边要增加它的权值 对于每一条非树边\(j\),他肯定与某些树边构成了一个环,那么它的边权必须大于等于这个环上的所有边 设其中一条边为\ ...
- CF741D Arpa’s letter-marked tree and Mehrdad’s Dokhtar-kosh paths(dsu on tree)
一棵根为1 的树,每条边上有一个字符(a-v共22种). 一条简单路径被称为Dokhtar-kosh当且仅当路径上的字符经过重新排序后可以变成一个回文串. 求每个子树中最长的Dokhtar-kosh路 ...
- [Swift通天遁地]一、超级工具-(2)制作美观大方的环形进度条
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★➤微信公众号:山青咏芝(shanqingyongzhi)➤博客园地址:山青咏芝(https://www.cnblogs. ...
- Coins HDU - 2844 POJ - 1742
Coins HDU - 2844 POJ - 1742 多重背包可行性 当做一般多重背包,二进制优化 #include<cstdio> #include<cstring> in ...
- 递推+高精度+找规律 UVA 10254 The Priest Mathematician
题目传送门 /* 题意:汉诺塔问题变形,多了第四个盘子可以放前k个塔,然后n-k个是经典的汉诺塔问题,问最少操作次数 递推+高精度+找规律:f[k]表示前k放在第四个盘子,g[n-k]表示经典三个盘子 ...
- paint之Graphics
在paint方法里面,这个Graphics类就相当于一支画笔.而且就画在那个component里面,比如frame. 看例子代码: import java.awt.*; public class Te ...
- windwsform登录页面
简单登录设计: 读取用户名密码 数据库表 实体类 数据访问类: 隐藏登录页面: 回车快捷键: 传值到main窗口:
- Java多线程——进程和线程
Java多线程——进程和线程 摘要:本文主要解释在Java这门编程语言中,什么是进程,什么是线程,以及二者之间的关系. 部分内容来自以下博客: https://www.cnblogs.com/dolp ...
- leetcode790 Domino and Tromino Tiling
思路: dp.没有像discuss中的那样优化递推式. 实现: class Solution { public: ; int numTilings(int N) { vector<vector& ...
- [Luogu1345][USACO5.4]Telecowmunication 最大流
题目链接:https://www.luogu.org/problem/show?pid=1345 求最小割点集的大小,直接拆点转化成最小割边.把一个点拆成出点入点,入点向出点连一条容量为1的边,其他的 ...