徐向博 / Imin.

阿里云短信api for php
作者:Imin 时间:2020-11-08 分类: PHP

代码如下:

<?php
    /**
     * 短信验证码
     */
    class Sms {
        // 保存错误信息
        public $error;

        // Access Key ID
        private $accessKeyId = '';

        // Access Access Key Secret
        private $accessKeySecret = '';

        // 签名
        private $signName = '';

        // 模版ID
        private $templateCode = '';

        // 构造函数
        public function __construct($cofig = array()) {
            $cofig = array (
                    'accessKeyId' => '***',
                    'accessKeySecret' => '***',
                    'signName' => '***',
                    'templateCode' => '***'
            );
            // 配置参数
            $this -> accessKeyId = $cofig ['accessKeyId'];
            $this -> accessKeySecret = $cofig ['accessKeySecret'];
            $this -> signName = $cofig ['signName'];
            $this -> templateCode = $cofig ['templateCode'];
        }

        // 序列化参数
        private function percentEncode($string) {
            $string = urlencode ( $string );
            $string = preg_replace ( '/\+/', '%20', $string );
            $string = preg_replace ( '/\*/', '%2A', $string );
            $string = preg_replace ( '/%7E/', '~', $string );
            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;
        }
        
        // 发送验证码
        public function send_verify($phone, $code) {
            $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' => $phone,
                'TemplateParam' => '{"code": '.$code.'}'
            );

            // 计算签名并把签名结果加入请求参数
            $params ['Signature'] = $this -> computeSignature ( $params, $this -> accessKeySecret );
            
            // 发送请求
            $url = 'http://dysmsapi.aliyuncs.com/?' . http_build_query ( $params );
    
            $ch = curl_init ();
            curl_setopt ( $ch, CURLOPT_URL, $url );
            curl_setopt ( $ch, CURLOPT_SSL_VERIFYPEER, false );
            curl_setopt ( $ch, CURLOPT_SSL_VERIFYHOST, false );
            curl_setopt ( $ch, CURLOPT_RETURNTRANSFER, 1 );
            $result = curl_exec ( $ch );
            curl_close ( $ch );
            return $result;
        }
    }
?>


调用:

 //生成验证码
    $phone = $_POST['phone'];
    $code = rand ( 100000, 999999 );
    $_SESSION['code'] = $code;

    //发送短信
    $sms = new Sms();
    $res = $sms -> send_verify($phone, $code);
    $res = json_decode($res, true);
    if ($res['Code'] == 'OK') {
        $message = array('code'=>'0','message'=>'发送成功,请注意查收');
        echo json_encode($message);
        exit();
    }else{
        $message = array('code'=>'-1','message'=> $res['Message']);
        echo json_encode($message);
        exit();
    }


本文标签: 阿里云短信

雷达探币: 腾不出时间来睡觉的人,迟早会腾出时间来生病;腾不出时间来恋爱的人,迟早会腾出时间来相亲 2021-09-17 13:49