RpcAcsRequest.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  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. /**
  21. * @deprecated See: https://github.com/aliyun/openapi-sdk-php
  22. * Class RpcAcsRequest
  23. */
  24. abstract class RpcAcsRequest extends AcsRequest
  25. {
  26. /**
  27. * @var string
  28. */
  29. private $dateTimeFormat = 'Y-m-d\TH:i:s\Z';
  30. /**
  31. * @var array
  32. */
  33. private $domainParameters = array();
  34. /**
  35. * @var string
  36. */
  37. protected $method = 'GET';
  38. /**
  39. * @var string
  40. */
  41. protected $acceptFormat = 'JSON';
  42. /**
  43. * @param string|bool $value
  44. *
  45. * @return string
  46. */
  47. private function prepareValue($value)
  48. {
  49. if (is_bool($value)) {
  50. if ($value) {
  51. return 'true';
  52. }
  53. return 'false';
  54. }
  55. return $value;
  56. }
  57. /**
  58. * @param $iSigner
  59. * @param $credential
  60. * @param $domain
  61. *
  62. * @return bool|mixed|string
  63. */
  64. public function composeUrl($iSigner, $credential, $domain)
  65. {
  66. $apiParams = parent::getQueryParameters();
  67. foreach ($apiParams as $key => $value) {
  68. $apiParams[$key] = $this->prepareValue($value);
  69. }
  70. $apiParams['RegionId'] = $this->getRegionId();
  71. $apiParams['AccessKeyId'] = $credential->getAccessKeyId();
  72. $apiParams['Format'] = $this->getAcceptFormat();
  73. $apiParams['SignatureMethod'] = $iSigner->getSignatureMethod();
  74. $apiParams['SignatureVersion'] = $iSigner->getSignatureVersion();
  75. if ($iSigner->getSignatureType() != null) {
  76. $apiParams['SignatureType'] = $iSigner->getSignatureType();
  77. }
  78. $apiParams['SignatureNonce'] = md5(uniqid(mt_rand(), true));
  79. $apiParams['Timestamp'] = gmdate($this->dateTimeFormat);
  80. $apiParams['Action'] = $this->getActionName();
  81. $apiParams['Version'] = $this->getVersion();
  82. if ($credential->getSecurityToken() != null) {
  83. $apiParams['SecurityToken'] = $credential->getSecurityToken();
  84. }
  85. if ($credential instanceof BearerTokenCredential) {
  86. $apiParams['BearerToken'] = $credential->getBearerToken();
  87. }
  88. $apiParams['Signature'] = $this->computeSignature($apiParams, $credential->getAccessSecret(), $iSigner);
  89. if (parent::getMethod() === 'POST') {
  90. $requestUrl = $this->getProtocol() . '://' . $domain . '/';
  91. foreach ($apiParams as $apiParamKey => $apiParamValue) {
  92. $this->putDomainParameters($apiParamKey, $apiParamValue);
  93. }
  94. return $requestUrl;
  95. }
  96. $requestUrl = $this->getProtocol() . '://' . $domain . '/?';
  97. foreach ($apiParams as $apiParamKey => $apiParamValue) {
  98. $requestUrl .= "$apiParamKey=" . urlencode($apiParamValue) . '&';
  99. }
  100. return substr($requestUrl, 0, -1);
  101. }
  102. /**
  103. * @param $parameters
  104. * @param $accessKeySecret
  105. * @param $iSigner
  106. *
  107. * @return mixed
  108. */
  109. private function computeSignature($parameters, $accessKeySecret, $iSigner)
  110. {
  111. ksort($parameters);
  112. $canonicalizedQueryString = '';
  113. foreach ($parameters as $key => $value) {
  114. $canonicalizedQueryString .= '&' . $this->percentEncode($key) . '=' . $this->percentEncode($value);
  115. }
  116. $this->stringToBeSigned =
  117. parent::getMethod() . '&%2F&' . $this->percentEncode(substr($canonicalizedQueryString, 1));
  118. return $iSigner->signString($this->stringToBeSigned, $accessKeySecret . '&');
  119. }
  120. /**
  121. * @param $str
  122. *
  123. * @return string|string[]|null
  124. */
  125. protected function percentEncode($str)
  126. {
  127. $res = urlencode($str);
  128. $res = str_replace(array('+', '*'), array('%20', '%2A'), $res);
  129. $res = preg_replace('/%7E/', '~', $res);
  130. return $res;
  131. }
  132. /**
  133. * @return array
  134. */
  135. public function getDomainParameter()
  136. {
  137. return $this->domainParameters;
  138. }
  139. /**
  140. * @param $name
  141. * @param $value
  142. */
  143. public function putDomainParameters($name, $value)
  144. {
  145. $this->domainParameters[$name] = $value;
  146. }
  147. }