一款大佬分享的简单方便阿里云短信封装代码,不用官网复杂的SDK,快速上手。可以拿来快速制作短信发送功能。具体代码如下: ?php/** * 阿里云SMS */namespace core\sms;use core\basic\Config;class AliyunSms { // 保存错误信息 public $error; // Access Key ID private $a
一款大佬分享的简单方便阿里云短信封装代码,不用官网复杂的SDK,快速上手。可以拿来快速制作短信发送功能。具体代码如下:
<?php
/**
* 阿里云SMS
*/
namespace core\sms;
use core\basic\Config;
class AliyunSms {
// 保存错误信息
public $error;
// Access Key ID
private $accessKeyId = '';
// Access Access Key Secret
private $accessKeySecret = '';
// 签名
private $signName = '';
// 模版ID
private $templateCode = '';
public function __construct() {
// 配置参数
$this->accessKeyId = Config::get('sms_appid');
$this->accessKeySecret = Config::get('sms_secret');
$this->signName = Config::get('sms_signame');
$this->templateCode = Config::get('sms_checkcodetpl');
}
private function percentEncode($string) {
$string = urlencode ( $string );
$string = preg_replace ( '/\+/', '%20', $string );
$string = preg_replace ( '/\*/', '%2A', $string );
$string = preg_replace ( '/%7E/', '~', $string );
return $string;
}
/**
* 签名
* @param unknown $parameters
* @param unknown $accessKeySecret
* @return string
*/
private function computeSignature($parameters, $accessKeySecret) {
ksort( $parameters );
$canonicalizedQueryString = '';
foreach ( $parameters as $key => $value ) {
$canonicalizedQueryString .= '&' . $this->percentEncode ( $key ) . '=' . $this->percentEncode ( $value );
}
$stringToSign = 'GET&%2F&' . $this->percentencode ( substr ( $canonicalizedQueryString, 1 ) );
$signature = base64_encode ( hash_hmac ( 'sha1', $stringToSign, $accessKeySecret . '&', true ) );
return $signature;
}
/**
* @param unknown $mobile
* @param unknown $verify_code
*
*/
public function send_verify($mobile, $tpljson) {
$params = array (
//此处作了修改
'SignName' => $this->signName,
'Format' => 'JSON',
'Version' => '2017-05-25',
'AccessKeyId' => $this->accessKeyId,
'SignatureVersion' => '1.0',
'SignatureMethod' => 'HMAC-SHA1',
'SignatureNonce' => uniqid (),
'Timestamp' => gmdate ( 'Y-m-d\TH:i:s\Z' ),
'Action' => 'SendSms',
'TemplateCode' => $this->templateCode,
'PhoneNumbers' => $mobile,
//'TemplateParam' => '{"code":"' . $verify_code . '"}'
'TemplateParam' => $tpljson //更换为自己的实际模版
);
//var_dump($params);die;
// 计算签名并把签名结果加入请求参数
$params ['Signature'] = $this->computeSignature( $params, $this->accessKeySecret );
// 发送请求(此处作了修改)
//$url = 'https://sms.aliyuncs.com/?' . http_build_query ( $params );
$url = 'http://dysmsapi.aliyuncs.com/?' . http_build_query ( $params );
$result = json_decode( get_url($url) );
if( isset($result->Code) && $result->Code=='OK' ) {
return ['code'=>1,'msg'=>$result->Message];
}else{
return ['code'=>0,'msg'=>$result->Message];
}
}
}
版权声明:本站部分内容来源互联网,如果文章中所涉及的图片或者文字内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!
沃梦达建站
PbootCMS的阿里云短信发送类
最新VIP资源
猜你喜欢
- PbootCMS后台常用文件修改路径位置 2023-04-03
- PbootCMS如何让客服在网站的留言直接邮件提醒 2023-04-14
- PbootCMS网站后台登录页面样式怎么修改 2023-03-23
- pbootcms怎么推送发布的文章到百度 2023-04-08
- pbootcms去除ueditor编辑器图片自动添加的title和alt属性 2023-04-03
- PbootCMS生成sitemap.xml中增加tag标签链接 2023-04-14
- PbootCMS网站常见错误提示总结 2023-04-11
- PbootCMS网站获取指定栏目下面所有单页内容办法 2023-03-30
- pbootcms为轮播图新增上传选项的办法 2023-03-18
- PbootCMS网站标题描述等标签限制字数的办法 2023-03-30