DefaultAcsClient.php 4.4KB

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