RpcAcsRequest.php 2.8 KB

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