RpcAcsRequest.php 2.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. <?php
  2. namespace Aliyun\Core;
  3. abstract class RpcAcsRequest extends AcsRequest
  4. {
  5. private $dateTimeFormat = 'Y-m-d\TH:i:s\Z';
  6. private $domainParameters = array();
  7. function __construct($product, $version, $actionName)
  8. {
  9. parent::__construct($product, $version, $actionName);
  10. $this->initialize();
  11. }
  12. private function initialize()
  13. {
  14. $this->setMethod("GET");
  15. $this->setAcceptFormat("JSON");
  16. }
  17. private function prepareValue($value)
  18. {
  19. if (is_bool($value)) {
  20. if ($value) {
  21. return "true";
  22. } else {
  23. return "false";
  24. }
  25. } else {
  26. return $value;
  27. }
  28. }
  29. public function composeUrl($iSigner, $credential, $domain)
  30. {
  31. $apiParams = parent::getQueryParameters();
  32. foreach ($apiParams as $key => $value) {
  33. $apiParams[$key] = $this->prepareValue($value);
  34. }
  35. $apiParams["RegionId"] = $this->getRegionId();
  36. $apiParams["AccessKeyId"] = $credential->getAccessKeyId();
  37. $apiParams["Format"] = $this->getAcceptFormat();
  38. $apiParams["SignatureMethod"] = $iSigner->getSignatureMethod();
  39. $apiParams["SignatureVersion"] = $iSigner->getSignatureVersion();
  40. $apiParams["SignatureNonce"] = uniqid(mt_rand(0,0xffff), true);
  41. $apiParams["Timestamp"] = gmdate($this->dateTimeFormat);
  42. $apiParams["Action"] = $this->getActionName();
  43. $apiParams["Version"] = $this->getVersion();
  44. $apiParams["Signature"] = $this->computeSignature($apiParams, $credential->getAccessSecret(), $iSigner);
  45. if(parent::getMethod() == "POST") {
  46. $requestUrl = $this->getProtocol()."://". $domain . "/";
  47. foreach ($apiParams as $apiParamKey => $apiParamValue)
  48. {
  49. $this->putDomainParameters($apiParamKey,$apiParamValue);
  50. }
  51. return $requestUrl;
  52. }
  53. else {
  54. $requestUrl = $this->getProtocol()."://". $domain . "/?";
  55. foreach ($apiParams as $apiParamKey => $apiParamValue)
  56. {
  57. $requestUrl .= "$apiParamKey=" . urlencode($apiParamValue) . "&";
  58. }
  59. return substr($requestUrl, 0, -1);
  60. }
  61. }
  62. private function computeSignature($parameters, $accessKeySecret, $iSigner)
  63. {
  64. ksort($parameters);
  65. $canonicalizedQueryString = '';
  66. foreach($parameters as $key => $value)
  67. {
  68. $canonicalizedQueryString .= '&' . $this->percentEncode($key). '=' . $this->percentEncode($value);
  69. }
  70. $stringToSign = parent::getMethod().'&%2F&' . $this->percentencode(substr($canonicalizedQueryString, 1));
  71. $signature = $iSigner->signString($stringToSign, $accessKeySecret."&");
  72. return $signature;
  73. }
  74. protected function percentEncode($str)
  75. {
  76. $res = urlencode($str);
  77. $res = preg_replace('/\+/', '%20', $res);
  78. $res = preg_replace('/\*/', '%2A', $res);
  79. $res = preg_replace('/%7E/', '~', $res);
  80. return $res;
  81. }
  82. public function getDomainParameter()
  83. {
  84. return $this->domainParameters;
  85. }
  86. public function putDomainParameters($name, $value)
  87. {
  88. $this->domainParameters[$name] = $value;
  89. }
  90. }