RpcAcsRequest.php 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. <?php
  2. namespace common\services\mall\aliyunsts\StsCode;
  3. /*
  4. * Licensed to the Apache Software Foundation (ASF) under one
  5. * or more contributor license agreements. See the NOTICE file
  6. * distributed with this work for additional information
  7. * regarding copyright ownership. The ASF licenses this file
  8. * to you under the Apache License, Version 2.0 (the
  9. * "License"); you may not use this file except in compliance
  10. * with the License. You may obtain a copy of the License at
  11. *
  12. * http://www.apache.org/licenses/LICENSE-2.0
  13. *
  14. * Unless required by applicable law or agreed to in writing,
  15. * software distributed under the License is distributed on an
  16. * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  17. * KIND, either express or implied. See the License for the
  18. * specific language governing permissions and limitations
  19. * under the License.
  20. */
  21. abstract class RpcAcsRequest extends AcsRequest
  22. {
  23. private $dateTimeFormat = 'Y-m-d\TH:i:s\Z';
  24. function __construct($product, $version, $actionName)
  25. {
  26. parent::__construct($product, $version, $actionName);
  27. $this->initialize();
  28. }
  29. private function initialize()
  30. {
  31. $this->setMethod("GET");
  32. $this->setAcceptFormat("JSON");
  33. }
  34. public function composeUrl($iSigner, $credential, $domain)
  35. {
  36. $apiParams = parent::getQueryParameters();
  37. $apiParams["RegionId"] = $this->getRegionId();
  38. $apiParams["AccessKeyId"] = $credential->getAccessKeyId();
  39. $apiParams["Format"] = $this->getAcceptFormat();
  40. $apiParams["SignatureMethod"] = $iSigner->getSignatureMethod();
  41. $apiParams["SignatureVersion"] = $iSigner->getSignatureVersion();
  42. $apiParams["SignatureNonce"] = uniqid();
  43. date_default_timezone_set("GMT");
  44. $apiParams["Timestamp"] = date($this->dateTimeFormat);
  45. $apiParams["Action"] = $this->getActionName();
  46. $apiParams["Version"] = $this->getVersion();
  47. $apiParams["Signature"] = $this->computeSignature($apiParams, $credential->getAccessSecret(), $iSigner);
  48. $requestUrl = $this->getProtocol()."://". $domain . "/?";
  49. foreach ($apiParams as $apiParamKey => $apiParamValue)
  50. {
  51. $requestUrl .= "$apiParamKey=" . urlencode($apiParamValue) . "&";
  52. }
  53. return substr($requestUrl, 0, -1);
  54. }
  55. private function computeSignature($parameters, $accessKeySecret, $iSigner)
  56. {
  57. ksort($parameters);
  58. $canonicalizedQueryString = '';
  59. foreach($parameters as $key => $value)
  60. {
  61. $canonicalizedQueryString .= '&' . $this->percentEncode($key). '=' . $this->percentEncode($value);
  62. }
  63. $stringToSign = parent::getMethod().'&%2F&' . $this->percentencode(substr($canonicalizedQueryString, 1));
  64. $signature = $iSigner->signString($stringToSign, $accessKeySecret."&");
  65. return $signature;
  66. }
  67. protected function percentEncode($str)
  68. {
  69. $res = urlencode($str);
  70. $res = preg_replace('/\+/', '%20', $res);
  71. $res = preg_replace('/\*/', '%2A', $res);
  72. $res = preg_replace('/%7E/', '~', $res);
  73. return $res;
  74. }
  75. }