RpcAcsRequest.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. <?php
  2. namespace app\common\services\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();
  41. date_default_timezone_set("GMT");
  42. $apiParams["Timestamp"] = date($this->dateTimeFormat);
  43. $apiParams["Action"] = $this->getActionName();
  44. $apiParams["Version"] = $this->getVersion();
  45. $apiParams["Signature"] = $this->computeSignature($apiParams, $credential->getAccessSecret(), $iSigner);
  46. if(parent::getMethod() == "POST") {
  47. $requestUrl = $this->getProtocol()."://". $domain . "/";
  48. foreach ($apiParams as $apiParamKey => $apiParamValue)
  49. {
  50. $this->putDomainParameters($apiParamKey,$apiParamValue);
  51. }
  52. return $requestUrl;
  53. }
  54. else {
  55. $requestUrl = $this->getProtocol()."://". $domain . "/?";
  56. foreach ($apiParams as $apiParamKey => $apiParamValue)
  57. {
  58. $requestUrl .= "$apiParamKey=" . urlencode($apiParamValue) . "&";
  59. }
  60. return substr($requestUrl, 0, -1);
  61. }
  62. }
  63. private function computeSignature($parameters, $accessKeySecret, $iSigner)
  64. {
  65. ksort($parameters);
  66. $canonicalizedQueryString = '';
  67. foreach($parameters as $key => $value)
  68. {
  69. $canonicalizedQueryString .= '&' . $this->percentEncode($key). '=' . $this->percentEncode($value);
  70. }
  71. $stringToSign = parent::getMethod().'&%2F&' . $this->percentencode(substr($canonicalizedQueryString, 1));
  72. $signature = $iSigner->signString($stringToSign, $accessKeySecret."&");
  73. return $signature;
  74. }
  75. protected function percentEncode($str)
  76. {
  77. $res = urlencode($str);
  78. $res = preg_replace('/\+/', '%20', $res);
  79. $res = preg_replace('/\*/', '%2A', $res);
  80. $res = preg_replace('/%7E/', '~', $res);
  81. return $res;
  82. }
  83. public function getDomainParameter()
  84. {
  85. return $this->domainParameters;
  86. }
  87. public function putDomainParameters($name, $value)
  88. {
  89. $this->domainParameters[$name] = $value;
  90. }
  91. }