RpcAcsRequest.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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. abstract class RpcAcsRequest extends AcsRequest
  22. {
  23. private $dateTimeFormat = 'Y-m-d\TH:i:s\Z';
  24. private $domainParameters = array();
  25. function __construct($product, $version, $actionName)
  26. {
  27. parent::__construct($product, $version, $actionName);
  28. $this->initialize();
  29. }
  30. private function initialize()
  31. {
  32. $this->setMethod("GET");
  33. $this->setAcceptFormat("JSON");
  34. }
  35. private function prepareValue($value)
  36. {
  37. if (is_bool($value)) {
  38. if ($value) {
  39. return "true";
  40. } else {
  41. return "false";
  42. }
  43. } else {
  44. return $value;
  45. }
  46. }
  47. public function composeUrl($iSigner, $credential, $domain)
  48. {
  49. $apiParams = parent::getQueryParameters();
  50. foreach ($apiParams as $key => $value) {
  51. $apiParams[$key] = $this->prepareValue($value);
  52. }
  53. $apiParams["RegionId"] = $this->getRegionId();
  54. $apiParams["AccessKeyId"] = $credential->getAccessKeyId();
  55. $apiParams["Format"] = $this->getAcceptFormat();
  56. $apiParams["SignatureMethod"] = $iSigner->getSignatureMethod();
  57. $apiParams["SignatureVersion"] = $iSigner->getSignatureVersion();
  58. $apiParams["SignatureNonce"] = uniqid();
  59. date_default_timezone_set("GMT");
  60. $apiParams["Timestamp"] = date($this->dateTimeFormat);
  61. $apiParams["Action"] = $this->getActionName();
  62. $apiParams["Version"] = $this->getVersion();
  63. $apiParams["Signature"] = $this->computeSignature($apiParams, $credential->getAccessSecret(), $iSigner);
  64. if(parent::getMethod() == "POST") {
  65. $requestUrl = $this->getProtocol()."://". $domain . "/";
  66. foreach ($apiParams as $apiParamKey => $apiParamValue)
  67. {
  68. $this->putDomainParameters($apiParamKey,$apiParamValue);
  69. }
  70. return $requestUrl;
  71. }
  72. else {
  73. $requestUrl = $this->getProtocol()."://". $domain . "/?";
  74. foreach ($apiParams as $apiParamKey => $apiParamValue)
  75. {
  76. $requestUrl .= "$apiParamKey=" . urlencode($apiParamValue) . "&";
  77. }
  78. return substr($requestUrl, 0, -1);
  79. }
  80. }
  81. private function computeSignature($parameters, $accessKeySecret, $iSigner)
  82. {
  83. ksort($parameters);
  84. $canonicalizedQueryString = '';
  85. foreach($parameters as $key => $value)
  86. {
  87. $canonicalizedQueryString .= '&' . $this->percentEncode($key). '=' . $this->percentEncode($value);
  88. }
  89. $stringToSign = parent::getMethod().'&%2F&' . $this->percentencode(substr($canonicalizedQueryString, 1));
  90. $signature = $iSigner->signString($stringToSign, $accessKeySecret."&");
  91. return $signature;
  92. }
  93. protected function percentEncode($str)
  94. {
  95. $res = urlencode($str);
  96. $res = preg_replace('/\+/', '%20', $res);
  97. $res = preg_replace('/\*/', '%2A', $res);
  98. $res = preg_replace('/%7E/', '~', $res);
  99. return $res;
  100. }
  101. public function getDomainParameter()
  102. {
  103. return $this->domainParameters;
  104. }
  105. public function putDomainParameters($name, $value)
  106. {
  107. $this->domainParameters[$name] = $value;
  108. }
  109. }