阿里云短信接口

配置文件

config.php

//阿里大鱼
'Ali_SMS' =>array(
'sms_temp' =>'短信模板',
'sms_sign' =>'签名',
'appkey' =>'appkey',
'secretKey'=>'secretKey',
),

TestController.php

Vendor('alisms.Alisms');
$alisms = new \Alisms(C('Ali_SMS.appkey'),C('Ali_SMS.secretKey'));
$mobile = $phone;
$temp_code = C('Ali_SMS.sms_temp'); $paramString = '{"code":"'.$code.'"}';
$alisms->signName = C('Ali_SMS.sms_sign');
$re = $alisms->smsend($mobile,$temp_code,$paramString);
if($re['Code'] =='OK'){
$info['status'] = 1;
$info['info'] = '短信发送成功!';
echo json_encode($info);
exit;
}else{
$info['info'] = '短信发送失败';
$info['status'] = 0;
echo json_encode($info);
exit;
}

Alisms.php

<?php

/**
* 阿里云短信接口
* @author 墨白<453885726@qq.com>
* 示例
* $alisms = new \Common\Model\Alisms($accessKeyId,$accessKeySecret);
* $mobile = '18788830181';
* $code = 'SMS_36225243';
* $paramString = '{"code":"344556"}';
* $re = $alisms->smsend($mobile,$code,$paramString);
* print_r($re);
*
*/ class Alisms{
public $config = array(
'Format' =>'json', //返回值的类型,支持JSON与XML。默认为XML
'Version' =>'2017-05-25', //API版本号,为日期形式:YYYY-MM-DD,本版本对应为2016-09-27
'SignatureMethod' =>'HMAC-SHA1', //签名方式,目前支持HMAC-SHA1
'SignatureVersion'=>'1.0',
);
private $accessKeySecret;
private $http = 'http://dysmsapi.aliyuncs.com';//https://sms.aliyuncs.com/'; //短信接口
private $dateTimeFormat = 'Y-m-d\TH:i:s\Z'; public $signName = '短信签名'; //管理控制台中配置的短信签名(状态必须是验证通过)
public $method = 'GET';
/**
*发送短信
*@AccessKeyId 阿里云申请的 Access Key ID
*@AccessKeySecret 阿里云申请的 Access Key Secret
*/
function __construct($accessKeyId,$accessKeySecret){
$this->config['AccessKeyId'] = $accessKeyId;
$this->AccessKeySecret = $accessKeySecret;
}
/**
*发送短信
*@mobile 目标手机号,多个手机号可以逗号分隔
*@code 短信模板的模板CODE
*@ParamString 短信模板中的变量;,参数格式{“no”:”123456”}, 个人用户每个变量长度必须小于15个字符
*/
public function smsend($mobile,$code,$ParamString){
$apiParams = $this->config;
$apiParams["Action"] = 'SendSms';//'SingleSendSms';
$apiParams['TemplateCode'] = $code; //短信模板的模板CODE
//$apiParams['RecNum'] = $mobile; //目标手机号,多个手机号可以逗号分隔
//$apiParams['ParamString'] = $ParamString; //短信模板中的变量;,此参数传递{“no”:”123456”}, 个人用户每个变量长度必须小于15个字符
$apiParams['SignName'] = $this->signName; //管理控制台中配置的短信签名(状态必须是验证通过)
date_default_timezone_set("GMT");
$apiParams["Timestamp"] = date($this->dateTimeFormat);
$apiParams["SignatureNonce"] = md5(md5('wbh').rand(100000,999999).uniqid()); //唯一随机数 $apiParams['RegionId'] = 'cn-hangzhou';
$apiParams['PhoneNumbers'] = $mobile;
$apiParams['TemplateParam'] = $ParamString; $apiParams["Signature"] = $this->computeSignature($apiParams, $this->AccessKeySecret);//签名 $tag = '?';
$requestUrl = $this->http;
foreach ($apiParams as $apiParamKey => $apiParamValue){
$requestUrl .= $tag."$apiParamKey=" . urlencode($apiParamValue);
$tag = '&';
}
return $this->postSMS($requestUrl);
}
private function postSMS($url){
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HEADER, 0);
$output = curl_exec($ch);
curl_close($ch);
return json_decode($output,true);
/*
$opts = array(
'http'=>array(
'method'=>$this->method,
'timeout'=>600,
'header'=>'Content-Type: application/x-www-form-urlencoded',
)
);
$html = file_get_contents($url, false, stream_context_create($opts));
if($html){
return json_decode($html,true);
}else{
return false;
}*/
} //生成取短信签名
private function computeSignature($parameters, $accessKeySecret){
ksort($parameters);
$canonicalizedQueryString = '';
foreach($parameters as $key => $value){
$canonicalizedQueryString .= '&' . $this->percentEncode($key). '=' . $this->percentEncode($value);
}
$stringToSign = $this->method.'&%2F&' . $this->percentencode(substr($canonicalizedQueryString, 1));
$signature = $this->signString($stringToSign, $accessKeySecret."&");
return $signature;
}
protected function percentEncode($str){
$res = urlencode($str);
$res = preg_replace('/\+/', '%20', $res);
$res = preg_replace('/\*/', '%2A', $res);
$res = preg_replace('/%7E/', '~', $res);
return $res;
}
private function signString($source, $accessSecret){
return base64_encode(hash_hmac('sha1', $source, $accessSecret, true));
}
}

TP3.2.3 接入阿里sms 短信接口的更多相关文章

  1. php与阿里云短信接口接入

    使用阿里云短信API,需要在控制台获取以下必要参数,其中需要自己手机验证+官方审核多次,尤其审核需要保持耐心. 1. accessKeyId  相当于你的个人账户密钥: 2. accessKeySec ...

  2. 阿里大鱼短信接口整合Tp3.2.3开发整理

    阿里大鱼 http://www.alidayu.com/ 的短信接口总体是不错的,别安驹个人认为不管是从性价比还是稳定性上都是跟同类的短信接口好些,毕竟是大公司的东西不会差到哪去.下面把之前开发的短信 ...

  3. asp.net mvc 接入阿里大于 短信验证码发送

    项目前端页面实例 第1步:登录阿里大于控制台 https://www.alidayu.com/center/user/account?spm=0.0.0.0.P1K1jG 第2步:创建应用 第3步:配 ...

  4. ThinkPHP3.2.3框架下接入阿里云短信服务接口实现:注册登录

    首先介绍下短信注册登录流程: 注册页面点击获取手机号验证码按钮,用jquery的click事件POST或GET方法把手机号发送到后台控制器: 后台控制器创建函数,收到手机号后生成随机码,例如:6位的随 ...

  5. 【微框架】Maven +SpringBoot 集成 阿里大鱼 短信接口详解与Demo

    Maven+springboot+阿里大于短信验证服务 纠结点:Maven库没有sdk,需要解决 Maven打包找不到相关类,需要解决 ps:最近好久没有写点东西了,项目太紧,今天来一篇 一.本文简介 ...

  6. 阿里云短信接口开发实践(Java

    随着互联网的兴起,各行各业的需求都在不断的增加.随着业务的扩大,企业给用户发送短信验证码的业务,也是如火如荼.在这里,calvin给各位开发者推荐阿里云短信平台.原因有二:1.接入较简单,开发成本低 ...

  7. TP5整合的阿里云短信接口

    现阶段,短信的应用主要就是用来验证下手机号是不是正常的手机号.只要涉及到用户手机号的问题的时候,都会做短信验证码来验证下改手机号是否是正常手机号.接下来就是操作步骤. 首先要在阿里云账号上开通短信功能 ...

  8. 中国网建SMS短信接口调用(java发送和接收手机短信)

    1.先注册账号,一定要填写好签名格式.不填会返回-51错误.   代码信息接口详细==>http://sms.webchinese.cn/api.shtml   . 2.测试代码 package ...

  9. THINKPHP3.2.3增加阿里云短信接口思路整理

    https://help.aliyun.com/document_detail/55359.html?spm=5176.product44282.4.7.O4lc1n 阿里云短信服务地址,感冒的下载看 ...

随机推荐

  1. Android 判断当前Fragment是否可见(Visible)

    判断当前Fragment是否可见 public abstract class BaseFragment extends Fragment { /** Fragment当前状态是否可见 */ prote ...

  2. 《ArcGIS Runtime SDK for Android开发笔记》——问题集:Error:Error: File path too long on Windows, keep below 240 characters

    1.前言 在使用Android Studio开发环境时,经常会爆出以下错误,虽然具体细节内容各有不同,但是说明的都是同一个问题,在windows中使用过长的路径,超过240字符. Error:Erro ...

  3. Jenkins新建节点,启动方式没有“通过Java Web启动代理”选项怎么办?

    在Jenkins中,打开“系统管理”→“管理节点”→“新建节点”页面时,“启动方式”选项没有“通过Java Web启动代理”,怎么办? 打开“系统管理”,进入“全局安全配置”页面. 1. “JNLP代 ...

  4. matlab练习程序(多线段交点)

    很简单的算法,这里是把每对线段都进行比较了. 还有一种似乎先通过x和y排序再进行交点判断的,不过那种方法我还没看太明白. 这里的方法如下: 1.根据线段的端点求两条直线的交点. 2.判断直线的交点是否 ...

  5. WebLogic配置与部署

    一.创建域: 第一步,选择“开始菜单”-> “Oracle WebLogic”-> “WebLogic Server 10gR3” -> “Tools”-> “Configur ...

  6. mac nvm install

    open the url https://github.com/creationix/nvm 1. curl -o- https://raw.githubusercontent.com/creatio ...

  7. Struts2学习-拦截器2

    1.做一个登陆页面(loginView.jsp,才用Action来访问),2.登陆成功后,可以跳转到系统的首页(index.jsp),3.首页有一个链接(testOtherAction访问其它的功能模 ...

  8. 笨办法学Python(三十九)

    习题 39: 列表的操作 你已经学过了列表.在你学习“while 循环”的时候,你对列表进行过“追加(append)”操作,而且将列表的内容打印了出来.另外你应该还在加分习题里研究过 Python 文 ...

  9. Oracle SYS用户无法设置session级别的read only

    官方文档参考:SYSDBA is used internally in the Oracle database and has specialized functions. Its behavior ...

  10. Facet constraits error: Spring 4.1 requires Java 1.6 or newer.

    问题来源: 在高版本的myeclipse,同步低版本的myeclipse提交的项目,可能会出现配置不一致. 问题描述: spring4.1不支持jdk1.6 注:在下载项目到本地的时候,myeclip ...