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 ...
随机推荐
- 476. Number Complement(补数)
Given a positive integer, output its complement number. The complement strategy is to flip the bits ...
- Codeforces Round #409(Div.2)
传送门 题意 A.询问最多改变一个字符的字符串"VK"子串数量 B.f(x,z)=y,给出x,y,求z For example, f("ab", "b ...
- 三种实现AJAX的方法以及Vue和axios结合使用的坑
前言 之前曾经想自己写一个天气的App,并找到了一个免费的天气数据接口的服务商--和风天气,当时也不懂怎么发HTTP请求,而且也只会用Java语言,看到官方文档里面有一个Java代码示例,就复制了一下 ...
- Java实现Excel数据批量导入数据库
Java实现Excel数据批量导入数据库 概述: 这个小工具类是工作中的一个小插曲哦,因为提数的时候需要跨数据库导数... 有的是需要从oracle导入mysql ,有的是从mysql导入oracle ...
- icons使用
1.将选中图标加入项目 2.unicode方式查看连接在线连接 3.复制代码到样式表 4.引用样式,并设置I标签,颜色和大小可以通过设置i标签color和font-size进行调整 <i cla ...
- linux mysql 8 安装
http://blog.itpub.net/31015730/viewspace-2152272/ https://blog.csdn.net/HaHa_Sir/article/details/805 ...
- B - Crossword solving
Erelong Leha was bored by calculating of the greatest common divisor of two factorials. Therefore he ...
- 【杂谈】RN的一点回顾与未来的展望
从开始到现在,笔者接触RN已经接近半年,适逢各种变化的发生,于是,简单的遐想了一下RN的未来. Airbnb在今年早些时候,宣布了放弃继续使用RN,并且发布了一篇“React Native at Ai ...
- Qt事件系统之一:Qt中的事件处理与传递
一.简介 在Qt中,事件作为一个对象,继承自 QEvent 类,常见的有键盘事件 QKeyEvent.鼠标事件 QMouseEvent 和定时器事件 QTimerEvent 等,与 QEvent 类的 ...
- LightOj 1220 Mysterious Bacteria
题目大意: 给出一个x,求满足x = b^p,p最大是多少? 解题思路: x可以表示为:x = p1^e1 * p2^e2 * p3^e3 ....... * pn^en. p = gcd (e1,e ...