PHP控制阿里云短信API接口实现短信群发功能
阿里云短信支持先使用后支付的原则,价格为4分半1条。
通过SDK可以与网站功能的绑定,实现响应的短信发送功能
现已统一合并升级为:消息服务。
消息服务
阿里云消息服务(Message Service,简称 MNS)是一种高效、可靠、安全、便捷、可弹性扩展的分布式消息服务。MNS能够帮助应用开发者在他们应用的分布式组件上自由的传递数据、通知消息,构建松耦合系统。
新的SDK下载地址:
Java SDK:https://help.aliyun.com/document_detail/51063.html
Python SDK:https://help.aliyun.com/document_detail/51372.html
C# SDK:https://help.aliyun.com/document_detail/52016.html
PHP SDK:https://help.aliyun.com/document_detail/51929.html
如果你像我一样是以前申请的短信服务升级过来的,那么旧的接口与API仍然能使用,新申请的只能使用新的SDK了。
本文旨在通过PHP实现阿里云短信接口,摆脱繁琐的SDK。
短信界面通过表单方式实现。
<div style="width:90%;margin:auto;margin-top:100px;text-align:center;font-size:12px;color:#192E32;">
<form method="post" action="">
<b>模板CODE:</b> <input type="text" name="tcode" id="tcode" required="required" value="SMS_12345678"
onFocus="if(value==defaultValue){value='';this.style.color='#000'}" onBlur="if(!value){value=defaultValue;this.style.color='#999'}"
oninput="myinput()" onpropertychange="myclick()" style="color:#999999;height:26px; width:150px; line-height:26px;" />
<span class="error" style="color:red;"> 阿里云短信模板编号</span>
<br><br>
<b>用户类型:</b>
<input type="radio" name="tuser" id="tuser" value="suppliers" checked="checked"; />供应商
<input type="radio" name="tuser" id="tuser" value="users" />全部用户
<span>
</span>
<br><br>
<input type="submit" name="sub" id="sub" value="发送短信" disabled="true" />
</form>
</div>
<script type="text/javascript">
function myinput(){
if(document.getElementById("tcode").value){
document.getElementById("sub").removeAttribute("disabled");
}
else{
document.getElementById("sub").setAttribute("disabled",disabled);
}
};
function myclick(){
if(document.getElementById("tcode").value){
document.getElementById("sub").setAttribute("disabled",false);
}
else{
document.getElementById("sub").setAttribute("disabled",true);
}
};
</script>
注意:阿里云短信模板编号为SMS_形式,通过JS控制输入提交按钮是否可用。
<?php define('IN_ECS', true);
require(dirname(__FILE__) . '/includes/init.php');
//ECShop初始化页面文件 function https_request($url)
{
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
$data = curl_exec($curl);
if (curl_errno($curl)) {return 'ERROR '.curl_error($curl);}
curl_close($curl);
return $data;
}
function xml_to_array($xml){
$reg = "/<(\w+)[^>]*>([\\x00-\\xFF]*)<\\/\\1>/";
if(preg_match_all($reg, $xml, $matches)){
$count = count($matches[0]);
for($i = 0; $i < $count; $i++){
$subxml= $matches[2][$i];
$key = $matches[1][$i];
if(preg_match( $reg, $subxml )){
$arr[$key] = xml_to_array( $subxml );
}else{
$arr[$key] = $subxml;
}
}
}
return @$arr;
}
function random($length = 6 , $numeric = 0) {
PHP_VERSION < '4.2.0' && mt_srand((double)microtime() * 1000000);
if($numeric) {
$hash = sprintf('%0'.$length.'d', mt_rand(0, pow(10, $length) - 1));
} else {
$hash = '';
$chars = 'ABCDEFGHJKLMNPQRSTUVWXYZ23456789abcdefghjkmnpqrstuvwxyz';
$max = strlen($chars) - 1;
for($i = 0; $i < $length; $i++) {
$hash .= $chars[mt_rand(0, $max)];
}
}
return $hash;
}
$target = "https://sms.aliyuncs.com/?";
//阿里云短信请求地址头
$mobile_code = random(6,1);
function percentEncode($str)
{
// 使用urlencode编码后,将"+","*","%7E"做替换即满足ECS API规定的编码规范
$res = urlencode($str);
$res = preg_replace('/\+/', '%20', $res);
$res = preg_replace('/\*/', '%2A', $res);
$res = preg_replace('/%7E/', '~', $res);
return $res;
}
function computeSignature($parameters, $accessKeySecret)
{
// 将参数Key按字典顺序排序
ksort($parameters);
// 生成规范化请求字符串
$canonicalizedQueryString = '';
foreach($parameters as $key => $value)
{
$canonicalizedQueryString .= '&' . percentEncode($key)
. '=' . percentEncode($value);
}
// 生成用于计算签名的字符串 stringToSign
$stringToSign = 'GET&%2F&' . percentencode(substr($canonicalizedQueryString, 1));
//echo "<br>".$stringToSign."<br>";
// 计算签名,注意accessKeySecret后面要加上字符'&'
$signature = base64_encode(hash_hmac('sha1', $stringToSign, $accessKeySecret . '&', true));
return $signature;
}
// 注意使用GMT时间
date_default_timezone_set("GMT");
$dateTimeFormat = 'Y-m-d\TH:i:s\Z'; // ISO8601规范
$accessKeyId = '根据实际修改'; // 这里填写您的Access Key ID
$accessKeySecret = '根据实际修改'; // 这里填写您的Access Key Secret
$ParamString="{\"code\":\"".strval($mobile_code)."\"}";
// 发送请求
if($_REQUEST['act'] == 'aliyun_mns'){// POST表单提交请求页面动作?act=aliyun_mns
/* 显示模板 */
if (isset($_POST['tcode']) && isset($_POST['tuser'])) {
$tcode=$_POST["tcode"];// 表单提交SMS短信模板
$tuser=$_POST["tuser"]; //表单提交用户组
$sql = "SELECT DISTINCT mobile_phone FROM " .$ecs->table($tuser);
$phoneNO = $db->getCol($sql);
$ii=0;
foreach($phoneNO as $key=>$pn)
{
if(preg_match("/^1[34578]\d{9}$/",$pn)){
$data = array(
// 公共参数
'SignName'=>'根据实际修改',
'Format' => 'XML',
'Version' => '2016-09-27',
'AccessKeyId' => $accessKeyId,
'SignatureVersion' => '1.0',
'SignatureMethod' => 'HMAC-SHA1',
'SignatureNonce'=> uniqid(),
'Timestamp' => date($dateTimeFormat),
// 接口参数
'Action' => 'SingleSendSms',
'TemplateCode' =>$tcode,
'RecNum' => $pn,
'ParamString' => $ParamString
);
$data['Signature'] = computeSignature($data, $accessKeySecret);
//给每一位用户发短信
$result = xml_to_array(https_request($target.http_build_query($data)));
}
}
}
/* 显示Smarty模板 */
assign_query_info();
$smarty->display('aliyun_mns.htm');
}
?>
创建和查看Access Key
https://ak-console.aliyun.com/#/accesskey/
YourSignName 和 YourSMSTemplateCode 获取在阿里云控制台消息服务。
PHP控制阿里云短信API接口实现短信群发功能的更多相关文章
- java 调用短信 api 接口发送短信
参考: https://blog.csdn.net/u014793522/article/details/59062014 参考 :https://blog.csdn.net/Lu_shilusi ...
- 如何防止短信API接口遍历
短信API接口在web中得到越来越多的应用,如用户注册,登录,密码重置等业务模块都会使用手机验证码进行身份验证.一般情况下,我们会采用这样的安全策略,将短信发送频率限制在正常的业务流控范围内,比如,一 ...
- 五:用JAVA写一个阿里云VPC Open API调用程序
用JAVA写一个阿里云VPC Open API调用程序 摘要:用JAVA拼出来Open API的URL 引言 VPC提供了丰富的API接口,让网络工程是可以通过API调用的方式管理网络资源.用程序和软 ...
- 试了下阿里云的OPEN Api
逐渐理解open api的意义,试了下阿里云的续费接口,续费一个月 package com.test; /** * @author * @date 2018/12/05 */ import com.a ...
- 阿里云视频直播API签名机制源码
阿里云视频直播API签名机制源码 本文展示:通过代码实现下阿里视频直播签名处理规则 阿里云视频直播签名机制,官方文档链接:https://help.aliyun.com/document_detail ...
- 关于新浪和腾讯短网址API接口的调用
最新新浪t.cn短网址和腾讯url.cn短网址生成api接口,快速生成t.cn及url.cn超短链接,接口都可以正常调用,觉得不错可以收藏一下. 新浪短网址api接口:1. http://yldwz. ...
- 如何利用新浪官方的短网址API接口实现T.cn短链接的压缩生成
短网址的实现原理就是有一个数据表会配置文件将短网址和实际网址进行对应,当请求某个短网址时,程序跳转到对应的实际网址上去,从而实现网址的访问.目前国内最稳定最好用的是新浪T.cn短链接. 之前新浪提供了 ...
- 阿里云ONS而微软Azure Service Bus体系结构和功能比较
阿里云ONS而微软Azure Service bus体系结构和功能比较 版权所有所有,转载请注明出处http://blog.csdn.net/yangzhenping.谢谢! 阿里云的开放消息服务: ...
- 用Python调用华为云API接口发短信
[摘要] 用Python调用华为云API接口实现发短信,当然能给调用发短信接口前提条件是通过企业实名认证,而且有一个通过审核的短信签名,话不多说,showcode #!/usr/bin/python3 ...
随机推荐
- Alpha冲刺 —— 个人总结
这几日Alpha冲刺的个人进展汇总,收获满满,我成长了. 我们的团队博客链接,团队作业第五次--Alpha冲刺 4.30 今日进展 改进数据库:字段命名重新规范,在record表中添加confirme ...
- Chisel3 - util - Queue
https://mp.weixin.qq.com/s/vlyOIsQxR6bCqDDMtRQLLg 实现队列模块,先入先出(FIFO). 参考链接: https://github.com/fr ...
- 数据库之 MySQL --- 数据处理 之 多行子查询(五)
[1]需求: 谁的工资比Abel 高?方式一: 第一步线查出Abel共子 第二部进行过滤 SELECT salary FROM employees WHERE last_name = 'Abel' ...
- 数据库之 MySQL --- 数据处理 之 单行函数、组函数 (四)
[1] LOWER : 将字符串中的内容全部转成小写 UPPER : 将字符串中的内容全部转成大写 SELECT LOWER ('abAcD') FROM DUAL SE ...
- Java实现 LeetCode 507 完美数
507. 完美数 对于一个 正整数,如果它和除了它自身以外的所有正因子之和相等,我们称它为"完美数". 给定一个 整数 n, 如果他是完美数,返回 True,否则返回 False ...
- Java实现 蓝桥杯VIP 算法提高 11-2删除重复元素
算法提高 11-2删除重复元素 时间限制:10.0s 内存限制:256.0MB 问题描述 为库设计新函数DelPack,删除输入字符串中所有的重复元素.不连续的重复元素也要删除. 要求写成函数,函数内 ...
- Java实现 LeetCode 72 编辑距离
72. 编辑距离 给定两个单词 word1 和 word2,计算出将 word1 转换成 word2 所使用的最少操作数 . 你可以对一个单词进行如下三种操作: 插入一个字符 删除一个字符 替换一个字 ...
- Javascript实现万年历(日历表)
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...
- Java 实现 蓝桥杯 等额本金
标题:等额本金 小明从银行贷款3万元.约定分24个月,以等额本金方式还款. 这种还款方式就是把贷款额度等分到24个月.每个月除了要还固定的本金外,还要还贷款余额在一个月 中产生的利息. 假设月利率是: ...
- idea 启动命令行的时候提示不能创建PTY
问题描述: 昨天上午,凯哥还在好好的使用idea,中午的时候,360扫描,好像要升级还是要干嘛的,没细看,然后凯哥就点击确定.结果到下午使用idea的Terminal 命令行的时候提示,如下图错误: ...