DefaultAcsClient.php 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  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 DefaultAcsClient
  23. */
  24. class DefaultAcsClient implements IAcsClient
  25. {
  26. /**
  27. * @var IClientProfile
  28. */
  29. public $iClientProfile;
  30. /**
  31. * @var bool
  32. */
  33. public $__urlTestFlag__;
  34. /**
  35. * @var LocationService
  36. */
  37. private $locationService;
  38. /**
  39. * @var RamRoleArnService
  40. */
  41. private $ramRoleArnService;
  42. /**
  43. * @var EcsRamRoleService
  44. */
  45. private $ecsRamRoleService;
  46. /**
  47. * DefaultAcsClient constructor.
  48. *
  49. * @param $iClientProfile
  50. */
  51. public function __construct($iClientProfile)
  52. {
  53. $this->iClientProfile = $iClientProfile;
  54. $this->__urlTestFlag__ = false;
  55. $this->locationService = new LocationService($this->iClientProfile);
  56. if ($this->iClientProfile->isRamRoleArn()) {
  57. $this->ramRoleArnService = new RamRoleArnService($this->iClientProfile);
  58. }
  59. if ($this->iClientProfile->isEcsRamRole()) {
  60. $this->ecsRamRoleService = new EcsRamRoleService($this->iClientProfile);
  61. }
  62. }
  63. /**
  64. * @param $request
  65. * @param null $iSigner
  66. * @param null $credential
  67. * @param bool $autoRetry
  68. * @param int $maxRetryNumber
  69. *
  70. * @return mixed|SimpleXMLElement
  71. * @throws ClientException
  72. * @throws ServerException
  73. */
  74. public function getAcsResponse($request,
  75. $iSigner = null,
  76. $credential = null,
  77. $autoRetry = true,
  78. $maxRetryNumber = 3)
  79. {
  80. $httpResponse = $this->doActionImpl($request, $iSigner, $credential, $autoRetry, $maxRetryNumber);
  81. $respObject = $this->parseAcsResponse($httpResponse->getBody(), $request->getAcceptFormat());
  82. if (false === $httpResponse->isSuccess()) {
  83. $this->buildApiException($respObject, $httpResponse->getStatus(), $request);
  84. }
  85. return $respObject;
  86. }
  87. /**
  88. * @param AcsRequest $request
  89. * @param null $iSigner
  90. * @param null $credential
  91. * @param bool $autoRetry
  92. * @param int $maxRetryNumber
  93. *
  94. * @return HttpResponse
  95. * @throws ClientException
  96. */
  97. private function doActionImpl($request, $iSigner = null, $credential = null, $autoRetry = true, $maxRetryNumber = 3)
  98. {
  99. if (null == $this->iClientProfile
  100. && (null == $iSigner || null == $credential
  101. || null == $request->getRegionId()
  102. || null == $request->getAcceptFormat())) {
  103. throw new ClientException('No active profile found.', 'SDK.InvalidProfile');
  104. }
  105. if (null == $iSigner) {
  106. $iSigner = $this->iClientProfile->getSigner();
  107. }
  108. if (null == $credential) {
  109. $credential = $this->iClientProfile->getCredential();
  110. }
  111. if ($this->iClientProfile->isRamRoleArn()) {
  112. $credential = $this->ramRoleArnService->getSessionCredential();
  113. }
  114. if ($this->iClientProfile->isEcsRamRole()) {
  115. $credential = $this->ecsRamRoleService->getSessionCredential();
  116. }
  117. if (null == $credential) {
  118. throw new ClientException('Incorrect user credentials.', 'SDK.InvalidCredential');
  119. }
  120. $request = $this->prepareRequest($request);
  121. // Get the domain from the Location Service by speicified `ServiceCode` and `RegionId`.
  122. $domain = null;
  123. if (null != $request->getLocationServiceCode()) {
  124. $domain =
  125. $this->locationService->findProductDomain($request->getRegionId(),
  126. $request->getLocationServiceCode(),
  127. $request->getLocationEndpointType(),
  128. $request->getProduct());
  129. }
  130. if ($domain == null) {
  131. $domain = EndpointProvider::findProductDomain($request->getRegionId(), $request->getProduct());
  132. }
  133. if (null == $domain) {
  134. throw new ClientException('Can not find endpoint to access.', 'SDK.InvalidRegionId');
  135. }
  136. $requestUrl = $request->composeUrl($iSigner, $credential, $domain);
  137. if ($this->__urlTestFlag__) {
  138. throw new ClientException($requestUrl, 'URLTestFlagIsSet');
  139. }
  140. if (count($request->getDomainParameter()) > 0) {
  141. $httpResponse =
  142. HttpHelper::curl($requestUrl,
  143. $request->getMethod(),
  144. $request->getDomainParameter(),
  145. $request->getHeaders());
  146. } else {
  147. $httpResponse =
  148. HttpHelper::curl($requestUrl, $request->getMethod(), $request->getContent(), $request->getHeaders());
  149. }
  150. $retryTimes = 1;
  151. while (500 <= $httpResponse->getStatus() && $autoRetry && $retryTimes < $maxRetryNumber) {
  152. $requestUrl = $request->composeUrl($iSigner, $credential, $domain);
  153. if (count($request->getDomainParameter()) > 0) {
  154. $httpResponse =
  155. HttpHelper::curl($requestUrl,
  156. $request->getMethod(),
  157. $request->getDomainParameter(),
  158. $request->getHeaders());
  159. } else {
  160. $httpResponse =
  161. HttpHelper::curl($requestUrl,
  162. $request->getMethod(),
  163. $request->getContent(),
  164. $request->getHeaders());
  165. }
  166. $retryTimes++;
  167. }
  168. return $httpResponse;
  169. }
  170. /**
  171. * @param AcsRequest $request
  172. * @param null $iSigner
  173. * @param null $credential
  174. * @param bool $autoRetry
  175. * @param int $maxRetryNumber
  176. *
  177. * @return HttpResponse|mixed
  178. * @throws ClientException
  179. */
  180. public function doAction($request, $iSigner = null, $credential = null, $autoRetry = true, $maxRetryNumber = 3)
  181. {
  182. trigger_error('doAction() is deprecated. Please use getAcsResponse() instead.', E_USER_NOTICE);
  183. return $this->doActionImpl($request, $iSigner, $credential, $autoRetry, $maxRetryNumber);
  184. }
  185. /**
  186. * @param $request
  187. *
  188. * @return mixed
  189. */
  190. private function prepareRequest($request)
  191. {
  192. if (null == $request->getRegionId()) {
  193. $request->setRegionId($this->iClientProfile->getRegionId());
  194. }
  195. if (null == $request->getAcceptFormat()) {
  196. $request->setAcceptFormat($this->iClientProfile->getFormat());
  197. }
  198. if (null == $request->getMethod()) {
  199. $request->setMethod('GET');
  200. }
  201. return $request;
  202. }
  203. /**
  204. * @param object $respObject
  205. * @param int $httpStatus
  206. *
  207. * @param AcsRequest $request
  208. *
  209. * @throws ServerException
  210. */
  211. private function buildApiException($respObject, $httpStatus, AcsRequest $request)
  212. {
  213. $errorCode = 'UnknownServerError';
  214. $errorMessage = 'The server returned an error without a detailed message. ';
  215. $requestId = 'None';
  216. // Compatible with different results
  217. if (isset($respObject->Message, $respObject->Code, $respObject->RequestId)) {
  218. $errorCode = $respObject->Code;
  219. $errorMessage = $respObject->Message;
  220. $requestId = $respObject->RequestId;
  221. }
  222. if (isset($respObject->message, $respObject->code, $respObject->requestId)) {
  223. $errorCode = $respObject->code;
  224. $errorMessage = $respObject->message;
  225. $requestId = $respObject->requestId;
  226. }
  227. if (isset($respObject->errorMsg, $respObject->errorCode)) {
  228. $errorCode = $respObject->errorCode;
  229. $errorMessage = $respObject->errorMsg;
  230. }
  231. if ($httpStatus === 400 && $errorCode === 'SignatureDoesNotMatch'
  232. && strpos($errorMessage,
  233. $request->stringToBeSigned()) !== false) {
  234. $errorCode = 'InvalidAccessKeySecret';
  235. $errorMessage = 'Specified Access Key Secret is not valid.';
  236. }
  237. throw new ServerException(
  238. $errorMessage,
  239. $errorCode,
  240. $httpStatus,
  241. $requestId
  242. );
  243. }
  244. /**
  245. * @param $body
  246. * @param $format
  247. *
  248. * @return mixed|SimpleXMLElement
  249. */
  250. private function parseAcsResponse($body, $format)
  251. {
  252. if ('JSON' === $format) {
  253. $respObject = json_decode($body);
  254. } elseif ('XML' === $format) {
  255. $respObject = @simplexml_load_string($body);
  256. } elseif ('RAW' === $format) {
  257. $respObject = $body;
  258. }
  259. return $respObject;
  260. }
  261. }