DefaultAcsClient.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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. namespace Mrgoon\AliyunSmsSdk;
  21. use GuzzleHttp\Exception\ClientException;
  22. use GuzzleHttp\Exception\ServerException;
  23. use Mrgoon\AliyunSmsSdk\Http\HttpHelper;
  24. use Mrgoon\AliyunSmsSdk\Regions\EndpointProvider;
  25. class DefaultAcsClient implements IAcsClient
  26. {
  27. public $iClientProfile;
  28. public $__urlTestFlag__;
  29. function __construct($iClientProfile)
  30. {
  31. $this->iClientProfile = $iClientProfile;
  32. $this->__urlTestFlag__ = false;
  33. }
  34. public function getAcsResponse($request, $iSigner = null, $credential = null, $autoRetry = true, $maxRetryNumber = 3)
  35. {
  36. $httpResponse = $this->doActionImpl($request, $iSigner, $credential, $autoRetry, $maxRetryNumber);
  37. $respObject = $this->parseAcsResponse($httpResponse->getBody(), $request->getAcceptFormat());
  38. if(false == $httpResponse->isSuccess())
  39. {
  40. $this->buildApiException($respObject, $httpResponse->getStatus());
  41. }
  42. return $respObject;
  43. }
  44. private function doActionImpl($request, $iSigner = null, $credential = null, $autoRetry = true, $maxRetryNumber = 3)
  45. {
  46. if(null == $this->iClientProfile && (null == $iSigner || null == $credential
  47. || null == $request->getRegionId() || null == $request->getAcceptFormat()))
  48. {
  49. throw new ClientException("No active profile found.", "SDK.InvalidProfile");
  50. }
  51. if(null == $iSigner)
  52. {
  53. $iSigner = $this->iClientProfile->getSigner();
  54. }
  55. if(null == $credential)
  56. {
  57. $credential = $this->iClientProfile->getCredential();
  58. }
  59. $request = $this->prepareRequest($request);
  60. $domain = EndpointProvider::findProductDomain($request->getRegionId(), $request->getProduct());
  61. if(null == $domain)
  62. {
  63. throw new ClientException("Can not find endpoint to access.", "SDK.InvalidRegionId");
  64. }
  65. $requestUrl = $request->composeUrl($iSigner, $credential, $domain);
  66. if ($this->__urlTestFlag__) {
  67. throw new ClientException($requestUrl, "URLTestFlagIsSet");
  68. }
  69. if(count($request->getDomainParameter())>0){
  70. $httpResponse = HttpHelper::curl($requestUrl, $request->getMethod(), $request->getDomainParameter(), $request->getHeaders());
  71. } else {
  72. $httpResponse = HttpHelper::curl($requestUrl, $request->getMethod(),$request->getContent(), $request->getHeaders());
  73. }
  74. $retryTimes = 1;
  75. while (500 <= $httpResponse->getStatus() && $autoRetry && $retryTimes < $maxRetryNumber) {
  76. $requestUrl = $request->composeUrl($iSigner, $credential,$domain);
  77. if(count($request->getDomainParameter())>0){
  78. $httpResponse = HttpHelper::curl($requestUrl, $request->getDomainParameter(), $request->getHeaders());
  79. } else {
  80. $httpResponse = HttpHelper::curl($requestUrl, $request->getMethod(), $request->getContent(), $request->getHeaders());
  81. }
  82. $retryTimes ++;
  83. }
  84. return $httpResponse;
  85. }
  86. public function doAction($request, $iSigner = null, $credential = null, $autoRetry = true, $maxRetryNumber = 3)
  87. {
  88. trigger_error("doAction() is deprecated. Please use getAcsResponse() instead.", E_USER_NOTICE);
  89. return $this->doActionImpl($request, $iSigner, $credential, $autoRetry, $maxRetryNumber);
  90. }
  91. private function prepareRequest($request)
  92. {
  93. if(null == $request->getRegionId())
  94. {
  95. $request->setRegionId($this->iClientProfile->getRegionId());
  96. }
  97. if(null == $request->getAcceptFormat())
  98. {
  99. $request->setAcceptFormat($this->iClientProfile->getFormat());
  100. }
  101. if(null == $request->getMethod())
  102. {
  103. $request->setMethod("GET");
  104. }
  105. return $request;
  106. }
  107. private function buildApiException($respObject, $httpStatus)
  108. {
  109. throw new ServerException($respObject->Message, $respObject->Code, $httpStatus, $respObject->RequestId);
  110. }
  111. private function parseAcsResponse($body, $format)
  112. {
  113. if ("JSON" == $format)
  114. {
  115. $respObject = json_decode($body);
  116. }
  117. else if("XML" == $format)
  118. {
  119. $respObject = @simplexml_load_string($body);
  120. }
  121. else if("RAW" == $format)
  122. {
  123. $respObject = $body;
  124. }
  125. return $respObject;
  126. }
  127. }