阿里云短信类最新版dysmsapi.aliyuncs.com

 $code];

        // 可选: 设置发送短信流水号
        //        $params['OutId'] = "12345";
        // 可选: 上行短信扩展码, 扩展码字段控制在7位或以下,无特殊需求用户请忽略此字段
        //        $params['SmsUpExtendCode'] = "1234567";
        // *** 需用户填写部分结束, 以下代码若无必要无需更改 ***
        if (!empty($params["TemplateParam"]) && is_array($params["TemplateParam"])) {
            $params["TemplateParam"] = json_encode($params["TemplateParam"], JSON_UNESCAPED_UNICODE);
        }
        $paramsArr = ["RegionId" => "cn-hangzhou", "Action" => "SendSms", "Version" => "2017-05-25",];
        // 此处可能会抛出异常,注意catch
        $content   = self::request("dysmsapi.aliyuncs.com", array_merge($params, $paramsArr));
        //var_dump($content);
        //exit;
        return $content;
    }

    /**
     * 发送通知
     * @param type $mobile
     * @param type $info
     * @param type $TemplateCode
     * @return type
     * ${consignee},购买的服务:${number},请及时查看!
     */
    public static function sendMessageSMS($mobile, $info = [], $TemplateCode = 'SMS_139228379')
    {

        $params = array();

        //手机号
        $params["PhoneNumbers"]  = $mobile;
        //签名
        $params["SignName"]      = self::$SignName;
        //登陆注册模板
        $params["TemplateCode"]  = $TemplateCode;
        //设置模板参数, 假如模板中存在变量需要替换则为必填项
        $params['TemplateParam'] = $info;

        // 可选: 设置发送短信流水号
        //        $params['OutId'] = "12345";
        // 可选: 上行短信扩展码, 扩展码字段控制在7位或以下,无特殊需求用户请忽略此字段
        //        $params['SmsUpExtendCode'] = "1234567";
        // *** 需用户填写部分结束, 以下代码若无必要无需更改 ***
        if (!empty($params["TemplateParam"]) && is_array($params["TemplateParam"])) {
            $params["TemplateParam"] = json_encode($params["TemplateParam"], JSON_UNESCAPED_UNICODE);
        }

        // 此处可能会抛出异常,注意catch
        $content = self::request("dysmsapi.aliyuncs.com", array_merge($params, $paramsArr));
        return $content;
    }

    public static function request($domain, $params, $security = false)
    {
        $accessKeyId     = self::$accessKeyId;
        $accessKeySecret = self::$accessKeySecret;

        $apiParams = array_merge(array(
            "SignatureMethod"  => "HMAC-SHA1",
            "SignatureNonce"   => uniqid(mt_rand(0, 0xffff), true),
            "SignatureVersion" => "1.0",
            "AccessKeyId"      => $accessKeyId,
            "Timestamp"        => gmdate("Y-m-d\TH:i:s\Z"),
            "Format"           => "JSON",
                ), $params);
        ksort($apiParams);

        $sortedQueryStringTmp = "";
        foreach ($apiParams as $key => $value) {
            $sortedQueryStringTmp .= "&" . self::encode($key) . "=" . self::encode($value);
        }

        $stringToSign = "GET&%2F&" . self::encode(substr($sortedQueryStringTmp, 1));

        $sign = base64_encode(hash_hmac("sha1", $stringToSign, $accessKeySecret . "&", true));

        $signature = self::encode($sign);

        $url = ($security ? 'https' : 'http') . "://{$domain}/?Signature={$signature}{$sortedQueryStringTmp}";

        try {
            $content = self::fetchContent($url);
            return json_decode($content, true);
        } catch (\Exception $e) {
            return false;
        }

        /**
         * 返回数据
         * {#201 ▼
          +"Message": "OK"
          +"RequestId": "2F3EFE06-AEA2-4CEE-ADAD-AA189C43C1F3"
          +"BizId": "410218722821144862^0"
          +"Code": "OK"
          }
         */
    }

    private static function encode($str)
    {
        $res = urlencode($str);
        $res = preg_replace("/\+/", "%20", $res);
        $res = preg_replace("/\*/", "%2A", $res);
        $res = preg_replace("/%7E/", "~", $res);
        return $res;
    }

    private static function fetchContent($url)
    {
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_TIMEOUT, 5);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_HTTPHEADER, array(
            "x-sdk-client" => "php/2.0.0",
        ));

        if (substr($url, 0, 5) == 'https') {
            curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
            curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
        }

        $rtn = curl_exec($ch);

        if ($rtn === false) {
            trigger_error("[CURL_" . curl_errno($ch) . "]: " . curl_error($ch), E_USER_ERROR);
        }
        curl_close($ch);

        return $rtn;
    }

    private static function random($length = 6, $numeric = 0)
    {
        PHP_VERSION < '4.2.0' && mt_srand((double) microtime() * 1000000);
        if ($numeric) {
            $hash = sprintf('%0' . $length . 'd', mt_rand(0, pow(10, $length) - 1));
        } else {
            $hash  = '';
            $chars = 'ABCDEFGHJKLMNPQRSTUVWXYZ23456789abcdefghjkmnpqrstuvwxyz';
            $max   = strlen($chars) - 1;
            for ($i = 0; $i < $length; $i++) {
                $hash .= $chars[mt_rand(0, $max)];
            }
        }
        return $hash;
    }

}

 

你可能感兴趣的:(php和mysqlweb开发)