php接入阿里短信发送
php接入阿里短信发送
短信发送提醒,一般用于验证码、消息通知等场景。度娘一搜也是一大把的开放平台,各个平台的收费也是不同,我这里选取阿里的记录下,其他平台方式差不多。
NO1:申请平台账号,阿里的账号申请后需要通过实名认证
NO2:账号有了,开通短消息发送功能,找到阿里的短信服务,点击右上侧accessKey,申请通过会生成一串ID值,一串密码值,记录下,后面有用
NO3:点击左侧栏菜单列表的快速学习,可以看到测试短信发送界面,内容中有两项需要申请
a:申请签名,签名可以使用自己网站,公司,APP应用名,刚开始我随便取了个名,审核不通过,。注意,该签名是必须的,并且审核过后,该签名会用于短信内容的最开头
b:申请模板,模板内可以添加自定义变量名,格式如下 ${name},该模板即为你发送的消息内容,变量内容可在代码内设置,注意,模板也是需要通过审核,生成对应的模板ID后,才能在代码内使用
NO4:以上步骤完成后,即能获取到开发中需要的所有参数。接入代码有两种方式,一是自行将参数处理好后调用阿里大鱼提供的消息发送接口,二是下载阿里提供的SDK,整合到代码内调用,我这里用的是第一种方式,个人觉得简单点,至于第二种方式自行百度,也不是很难的说
NO5:特别注意哈,这个接口是收费的,5000条起售,二百大洋左右;
上代码,定义类Alisms,处理发送信息,代码内容如下:
<?php
/***
* 阿里云短信发送接口
***/
class Alisms{
public $config = array(
'Format' => 'json', //返回值类型,支持json和xml,默认为xml
'SignatureMethod' =>'HMAC-SHA1', //签名方式,目前支持HMAC-SHA1
'SignatureVersion' =>'1.0', //签名版本号
'Version' => '2017-05-25', //版本日期
private $http = 'http://dysmsapi.aliyuncs.com'; //短信发送调用接口
private $dateTimeFormat = 'Y-m-d\TH:i:s\Z'; //格式化日期
public $signName = '阿里云'; //NO3步骤a申请的短信签名
//发送短信构造函数
function __construct($accessKey, $accessKeySecret){
$this->config['AccessKeyId'] = $accessKey;
$this->AccessKeySecret = $accessKeySecret;
}
//短信发送接口
public function msgSend($mobile, $code, $paramStr){
date_default_timezone_set("GMT"); //格林尼
$apiParams = $this->config;
$apiParams['Action'] = 'SendSms'; //请求接口标志
$apiParams['TemplateCode'] = $code; //审核通过的短信模板ID
$apiParams['PhoneNumbers'] = $mobile; //发送目标手机号,多个使用逗号分隔
$apiParams['SignName'] = $this->signName; //短信签名
$apiParams['TemplateParam'] = $paramStr; //传入模板中变量 格式{"name":"XXX"},多个用逗号隔开
$apiParams['RegionId'] = 'cn-hangzhou'; //地域标志
$apiParams['Timestamp'] = date($this->dateTimeFormat); //时间
$apiParams['SignatureNonce'] = md5(md5('yyq'),rand(100000, 999999).uniqid()); //唯一随机数
$apiParams['Signature'] = $this->getSignature($apiParams, $this->AccessKeySecret); //参数签名
//参数处理
$tag = '?';
$requestUrl = $this->http;
foreach($apiParams as $apiKey => $apiVal){
$requestUrl .= $tag."$apiKey=". urlencode($apiVal);
$tag = '&';
}
return $this->postSms($requestUrl);
}
//curl发送并返回数据
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);
$ret_out = json_encode($output, true);
return $ret_out;
}
//生成取短信签名
private function getSignature($paramArr, $accessKeySecret){
ksort($paramArr);
$queryStr = '';
foreach($paramArr as $key=>$value){
$queryStr .= '&' . $this->percentEncode($key) . '=' . $this->percentEncode($value);
}
$stringToSign = 'GET&%2F&' . $this->percentEncode(substr($queryStr, 1));
$signature = base64_encode(hash_hmac('sha1', $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;
}
//==========分割线=============
以下代码为调用测试:
require_once('Alisms.php'); //引入文件地址,路径自行设置
$appkey = 'aaaa'; //阿里云申请的accessKeyId
$appsec = 'bbbb'; //阿里云申请的accessKeySecret
$tmpCode = 'SMS_000000000'; //审核通过的模板名称
$sms_sign = 'AAAA'; //审核通过的签名
$mobile = '13500000000'; //发送目标号码
$name = 'yyq'; //模板变量1 值
$product = 'product'; //模板变量2 值
$paramString = '{"name":"'.$name.'", "product":"'.$product.'"}'; //传递模板变量格式,格外注意写法,否则阿里识别不出
//new个对象
$alisms = new \Alisms($appkey, $appsec);
$alisms->signName = $sms_sign; //设置签名
$ret = $alisms->msgSend(); //调用发送接口
//判断返回结果
if($ret['Code'] == 'OK'){
echo '发送成功';
}else{
echo '发送失败';
}
//===================end====================