zoukankan      html  css  js  c++  java
  • amazon 签名类

    此类用于调用WMS APi时请求参数生成签名

    <?php
    
    
    namespace amazon;
    
    
    class Signature
    {
        /**
         * The signed string.
         *
         * @var string
         */
        protected $signedString;
    
        /**
         * Create a new signature instance.
         *
         * @param  string  $url
         * @param  array   $data
         * @param  string  $secretAccessKey
         */
        public function __construct($url, array $parameters, $secretAccessKey)
        {
            $stringToSign = $this->calculateStringToSign($url, $parameters);
    
            $this->signedString = $this->sign($stringToSign, $secretAccessKey);
        }
    
        /**
         * Calculate the string to sign.
         *
         * @param  string  $url
         * @param  array   $parameters
         * @return string
         */
        protected function calculateStringToSign($url, array $parameters)
        {
            $url = parse_url($url);
    
            $string = "POST
    ";
            $string .= $url['host']."
    ";
            $string .= $url['path']."
    ";
            $string .= $this->getParametersAsString($parameters);
    
            return $string;
        }
    
        /**
         * Computes RFC 2104-compliant HMAC signature.
         *
         * @param  string  $data
         * @param  string  $secretAccessKey
         * @return string
         */
        protected function sign($data, $secretAccessKey)
        {
            return base64_encode(hash_hmac('sha256', $data, $secretAccessKey, true));
        }
    
        /**
         * Convert paremeters to URL-encoded query string.
         *
         * @param  array  $parameters
         * @return string
         */
        protected function getParametersAsString(array $parameters)
        {
            uksort($parameters, 'strcmp');
    
            $queryParameters = [];
    
            foreach ($parameters as $key => $value) {
                $key = rawurlencode($key);
                $value = rawurlencode($value);
    
                $queryParameters[] = sprintf('%s=%s', $key, $value);
            }
    
            return implode('&', $queryParameters);
        }
    
        /**
         * The string representation of this signature.
         *
         * @return string
         */
        public function __toString()
        {
            return $this->signedString;
        }
    
    }
    你优秀了,自然有对的人与你并肩,错的人迟早走散,而相爱的终将再相逢。
  • 相关阅读:
    通过JDBC连接oracle数据库的十大技巧
    asp.net里导出excel表方法汇总
    流式媒体
    html 另存为/打印/刷新/查看原文件等 代码
    JSP 语法详解
    让一个网页打开的同时自动为另一个网页自动填写表单并提交
    StreamWriter输出中文乱码的问题
    “mailto”的六则应用技巧
    返回目录大小
    JDBC基础(二)
  • 原文地址:https://www.cnblogs.com/szrhsd/p/14068992.html
Copyright © 2011-2022 走看看