您现在的位置是:首页 > cms教程 > PbootCMS教程PbootCMS教程

PbootCMS对接阿里云短信发送类的方法

友巧2023-01-25PbootCMS教程已有人查阅

导读一款大佬分享的简单方便阿里云短信封装代码,不用官网复杂的SDK,快速上手。由CMS88适配成pbootcms,可以拿来快速制作短信发送功能。

一款大佬分享的简单方便阿里云短信封装代码,不用官网复杂的SDK,快速上手。由CMS88适配成pbootcms,可以拿来快速制作短信发送功能。
封装代码
<?php
/**
 * @author CMS88
 *  阿里云SMS
 */
namespace coresms;
use corebasicConfig;
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-dTH:i:sZ' ),
            '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源码下载为律师事务所、法律咨询机构设计,特别适合展示法律服务、律师团队和成功案例。采用响应式技术,确保在不同设备上都能提供专业的法律信息展示和咨询服务。查看源码
  • (自适应HTML5)响应式智能设备人工智能机器pbootcms源码免费下载这款基于PbootCMS开发的网站模板为人工智能和智能设备行业设计,采用现代化科技风格,突出产品的智能特性和技术创新。模板架构合理,功能完善,能够有效展示各类智能产品的核心功能和解决方案。查看源码
  • (自适应响应式)html5蓝色智能水表营销型网站pbootcms模板下载PbootCMS内核开发,为智能水表企业打造的营销型网站解决方案,本模板基于PbootCMS内核开发,为智能水表及相关行业企业设计,采用HTML5+CSS3技术构建,具有响应式布局。查看源码
  • 帝国CMS中小学生知识点试题练习题考试源码下载基于帝国CMS后台管理系统,支持在线修改联系信息、网站参数等配置,操作流程简洁明了。专注于中小学教育领域,集成知识点总结、试题练习、考试资讯等学习资源,为师生提供全面的在线学习支持与服务。查看源码
  • pbootcms模板PC+WAP娱乐新闻资讯类博客网站源码该模板基于PbootCMS内核开发,专为娱乐新闻、健康生活类资讯网站设计,同时支持快速适配其他行业(如企业官网、博客门户等),仅需替换图文内容即可完成转型。查看源码
  • (自适应)html5导航目录索引工具类网站源码下载本模板基于PbootCMS开发,为站长导航、网址导航类网站设计。采用HTML5+CSS3技术构建,适配PC端和移动端,提供流畅的浏览体验。适用于个人站长、资源导航平台等场景查看源码
分享笔记 (共有 篇笔记)
验证码: