阿里大于短信接口

@fright  November 22, 2017
    
/**
     * 阿里大于发送短信
     * @param $recNum       发送手机号
     * @param $paramString   模板变量(json)
     * @param $templateCode  模板号
     * @param $signName  签名
     * @return array
     */
public static function sendOneDy($recNum, $paramString, $templateCode, $signName)
        {
            
            date_default_timezone_set("GMT");
            $accessKeySecret = '阿里大于accessKeySecret ';
            $accessKeyId = '阿里大于accessKeyId ';
            $array = [
                'PhoneNumbers' => $recNum,
                'SignName' => $signName,
                'TemplateCode' => $templateCode,
                'TemplateParam' => $paramString,
                'RegionId' => 'cn-hangzhou',
                'AccessKeyId' => $accessKeyId,
                'Format' => 'JSON',
                'SignatureMethod' => 'HMAC-SHA1',
                'SignatureVersion' => '1.0',
                'SignatureNonce' => uniqid(mt_rand(0,0xffff), true),
                'Timestamp' => date('Y-m-d\TH:i:s\Z'),
                'Action' => 'SendSms',
                'Version' => '2017-05-25',
            ];
            $array['Signature'] = self::computeSignatureDy($array, $accessKeySecret);
            $requestUrl = 'http://dysmsapi.aliyuncs.com';
            // 发送请求
            $result = Common::postCurl($requestUrl,$array);  //curl自己封装一个
            $obj = json_decode($result);
            if ($obj->Code == 'OK'){
                $res = [
                    'state' => true,
                    'message' => $result
                ];
            }else{
                $res = [
                    'state' => false,
                    'message' => $result
                ];
            }
            return $res;
        }
        public static function computeSignatureDy($parameters, $accessKeySecret){
            ksort($parameters);
            $canonicalizedQueryString = '';
            foreach($parameters as $key => $value)
            {
                $canonicalizedQueryString .= '&' . self::percentEncodeDy($key). '=' . self::percentEncodeDy($value);
            }
            $stringToSign = 'POST&%2F&' . self::percentEncodeDy(substr($canonicalizedQueryString, 1));
            $signature = base64_encode(hash_hmac('sha1', $stringToSign, $accessKeySecret . '&', true));
            return $signature;
        }
        public static function percentEncodeDy($str)
        {
            $res = urlencode($str);
            $res = preg_replace('/\+/', '%20', $res);
            $res = preg_replace('/\*/', '%2A', $res);
            $res = preg_replace('/%7E/', '~', $res);
            return $res;
        }

评论已关闭