Request.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. <?php
  2. require_once QCLOUDAPI_ROOT_PATH . '/Common/Sign.php';
  3. /**
  4. * QcloudApi_Common_Request
  5. */
  6. class QcloudApi_Common_Request
  7. {
  8. /**
  9. * $_requestUrl
  10. * 请求url
  11. * @var string
  12. */
  13. protected static $_requestUrl = '';
  14. /**
  15. * $_rawResponse
  16. * 原始的返回信息
  17. * @var string
  18. */
  19. protected static $_rawResponse = '';
  20. /**
  21. * $_version
  22. * @var string
  23. */
  24. protected static $_version = 'SDK_PHP_1.1';
  25. /**
  26. * $_timeOut
  27. * 设置连接主机的超时时间
  28. * @var int 数量级:秒
  29. * */
  30. protected static $_timeOut = 10;
  31. /**
  32. * getRequestUrl
  33. * 获取请求url
  34. */
  35. public static function getRequestUrl()
  36. {
  37. return self::$_requestUrl;
  38. }
  39. /**
  40. * getRawResponse
  41. * 获取原始的返回信息
  42. */
  43. public static function getRawResponse()
  44. {
  45. return self::$_rawResponse;
  46. }
  47. /**
  48. * generateUrl
  49. * 生成请求的URL
  50. *
  51. * @param array $paramArray
  52. * @param string $secretId
  53. * @param string $secretKey
  54. * @param string $requestHost
  55. * @param string $requestPath
  56. * @param string $requestMethod
  57. * @return
  58. */
  59. public static function generateUrl($paramArray, $secretId, $secretKey, $requestMethod, $requestHost, $requestPath) {
  60. if(!isset($paramArray['SecretId']))
  61. $paramArray['SecretId'] = $secretId;
  62. if (!isset($paramArray['Nonce']))
  63. $paramArray['Nonce'] = rand(1, 65535);
  64. if (!isset($paramArray['Timestamp']))
  65. $paramArray['Timestamp'] = time();
  66. $paramArray['RequestClient'] = self::$_version;
  67. $plainText = QcloudApi_Common_Sign::makeSignPlainText($paramArray,
  68. $requestMethod, $requestHost, $requestPath);
  69. $paramArray['Signature'] = QcloudApi_Common_Sign::sign($plainText, $secretKey);
  70. $url = 'https://' . $requestHost . $requestPath;
  71. if ($requestMethod == 'GET') {
  72. $url .= '?' . http_build_query($paramArray);
  73. }
  74. return $url;
  75. }
  76. /**
  77. * send
  78. * 发起请求
  79. * @param array $paramArray 请求参数
  80. * @param string $secretId secretId
  81. * @param string $secretKey secretKey
  82. * @param string $requestMethod 请求方式,GET/POST
  83. * @param string $requestHost 接口域名
  84. * @param string $requestPath url路径
  85. * @return
  86. */
  87. public static function send($paramArray, $secretId, $secretKey, $requestMethod, $requestHost, $requestPath)
  88. {
  89. if(!isset($paramArray['SecretId']))
  90. $paramArray['SecretId'] = $secretId;
  91. if (!isset($paramArray['Nonce']))
  92. $paramArray['Nonce'] = rand(1, 65535);
  93. if (!isset($paramArray['Timestamp']))
  94. $paramArray['Timestamp'] = time();
  95. $paramArray['RequestClient'] = self::$_version;
  96. $plainText = QcloudApi_Common_Sign::makeSignPlainText($paramArray,
  97. $requestMethod, $requestHost, $requestPath);
  98. $paramArray['Signature'] = QcloudApi_Common_Sign::sign($plainText, $secretKey);
  99. $url = 'https://' . $requestHost . $requestPath;
  100. $ret = self::_sendRequest($url, $paramArray, $requestMethod);
  101. return $ret;
  102. }
  103. /**
  104. * _sendRequest
  105. * @param string $url 请求url
  106. * @param array $paramArray 请求参数
  107. * @param string $method 请求方法
  108. * @return
  109. */
  110. protected static function _sendRequest($url, $paramArray, $method = 'POST')
  111. {
  112. $ch = curl_init();
  113. if ($method == 'POST')
  114. {
  115. $paramArray = is_array( $paramArray ) ? http_build_query( $paramArray ) : $paramArray;
  116. curl_setopt($ch, CURLOPT_POST, 1);
  117. curl_setopt($ch, CURLOPT_POSTFIELDS, $paramArray);
  118. }
  119. else
  120. {
  121. $url .= '?' . http_build_query($paramArray);
  122. }
  123. self::$_requestUrl = $url;
  124. curl_setopt($ch, CURLOPT_URL, $url);
  125. curl_setopt($ch, CURLOPT_TIMEOUT,self::$_timeOut);
  126. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  127. if (false !== strpos($url, "https")) {
  128. // 证书
  129. // curl_setopt($ch,CURLOPT_CAINFO,"ca.crt");
  130. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
  131. curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
  132. }
  133. $resultStr = curl_exec($ch);
  134. self::$_rawResponse = $resultStr;
  135. $result = json_decode($resultStr, true);
  136. if (!$result)
  137. {
  138. return $resultStr;
  139. }
  140. return $result;
  141. }
  142. }