Client.php 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. <?php
  2. namespace Qiniu\Http;
  3. use Qiniu\Config;
  4. use Qiniu\Http\Middleware;
  5. final class Client
  6. {
  7. /**
  8. * @param $url
  9. * @param array $headers
  10. * @param RequestOptions $opt
  11. * @return Response
  12. */
  13. public static function get($url, array $headers = array(), $opt = null)
  14. {
  15. $request = new Request('GET', $url, $headers, null, $opt);
  16. return self::sendRequestWithMiddleware($request);
  17. }
  18. /**
  19. * @param $url
  20. * @param array $headers
  21. * @param array $opt detail see {@see Request::$opt}
  22. * @return Response
  23. */
  24. public static function delete($url, array $headers = array(), $opt = null)
  25. {
  26. $request = new Request('DELETE', $url, $headers, null, $opt);
  27. return self::sendRequest($request);
  28. }
  29. /**
  30. * @param $url
  31. * @param $body
  32. * @param array $headers
  33. * @param RequestOptions $opt
  34. * @return Response
  35. */
  36. public static function post($url, $body, array $headers = array(), $opt = null)
  37. {
  38. $request = new Request('POST', $url, $headers, $body, $opt);
  39. return self::sendRequest($request);
  40. }
  41. /**
  42. * @param $url
  43. * @param $body
  44. * @param array $headers
  45. * @param RequestOptions $opt
  46. * @return Response
  47. */
  48. public static function PUT($url, $body, array $headers = array(), $opt = null)
  49. {
  50. $request = new Request('PUT', $url, $headers, $body, $opt);
  51. return self::sendRequest($request);
  52. }
  53. /**
  54. * @param $url
  55. * @param array $fields
  56. * @param string $name
  57. * @param string $fileName
  58. * @param $fileBody
  59. * @param null $mimeType
  60. * @param array $headers
  61. * @param RequestOptions $opt
  62. * @return Response
  63. */
  64. public static function multipartPost(
  65. $url,
  66. $fields,
  67. $name,
  68. $fileName,
  69. $fileBody,
  70. $mimeType = null,
  71. $headers = array(),
  72. $opt = null
  73. ) {
  74. $data = array();
  75. $mimeBoundary = md5(microtime());
  76. foreach ($fields as $key => $val) {
  77. array_push($data, '--' . $mimeBoundary);
  78. array_push($data, "Content-Disposition: form-data; name=\"$key\"");
  79. array_push($data, '');
  80. array_push($data, $val);
  81. }
  82. array_push($data, '--' . $mimeBoundary);
  83. $finalMimeType = empty($mimeType) ? 'application/octet-stream' : $mimeType;
  84. $finalFileName = self::escapeQuotes($fileName);
  85. array_push($data, "Content-Disposition: form-data; name=\"$name\"; filename=\"$finalFileName\"");
  86. array_push($data, "Content-Type: $finalMimeType");
  87. array_push($data, '');
  88. array_push($data, $fileBody);
  89. array_push($data, '--' . $mimeBoundary . '--');
  90. array_push($data, '');
  91. $body = implode("\r\n", $data);
  92. $contentType = 'multipart/form-data; boundary=' . $mimeBoundary;
  93. $headers['Content-Type'] = $contentType;
  94. $request = new Request('POST', $url, $headers, $body, $opt);
  95. return self::sendRequest($request);
  96. }
  97. private static function userAgent()
  98. {
  99. $sdkInfo = "QiniuPHP/" . Config::SDK_VER;
  100. $systemInfo = php_uname("s");
  101. $machineInfo = php_uname("m");
  102. $envInfo = "($systemInfo/$machineInfo)";
  103. $phpVer = phpversion();
  104. $ua = "$sdkInfo $envInfo PHP/$phpVer";
  105. return $ua;
  106. }
  107. /**
  108. * @param Request $request
  109. * @return Response
  110. */
  111. public static function sendRequestWithMiddleware($request)
  112. {
  113. $middlewares = $request->opt->middlewares;
  114. $handle = Middleware\compose($middlewares, function ($req) {
  115. return Client::sendRequest($req);
  116. });
  117. return $handle($request);
  118. }
  119. /**
  120. * @param Request $request
  121. * @return Response
  122. */
  123. public static function sendRequest($request)
  124. {
  125. $t1 = microtime(true);
  126. $ch = curl_init();
  127. $options = array(
  128. CURLOPT_USERAGENT => self::userAgent(),
  129. CURLOPT_RETURNTRANSFER => true,
  130. CURLOPT_HEADER => true,
  131. CURLOPT_NOBODY => false,
  132. CURLOPT_CUSTOMREQUEST => $request->method,
  133. CURLOPT_URL => $request->url,
  134. );
  135. foreach ($request->opt->getCurlOpt() as $k => $v) {
  136. $options[$k] = $v;
  137. }
  138. // Handle open_basedir & safe mode
  139. if (!ini_get('safe_mode') && !ini_get('open_basedir')) {
  140. $options[CURLOPT_FOLLOWLOCATION] = true;
  141. }
  142. if (!empty($request->headers)) {
  143. $headers = array();
  144. foreach ($request->headers as $key => $val) {
  145. array_push($headers, "$key: $val");
  146. }
  147. $options[CURLOPT_HTTPHEADER] = $headers;
  148. }
  149. curl_setopt($ch, CURLOPT_HTTPHEADER, array('Expect:'));
  150. if (!empty($request->body)) {
  151. $options[CURLOPT_POSTFIELDS] = $request->body;
  152. }
  153. curl_setopt_array($ch, $options);
  154. $result = curl_exec($ch);
  155. $t2 = microtime(true);
  156. $duration = round($t2 - $t1, 3);
  157. $ret = curl_errno($ch);
  158. if ($ret !== 0) {
  159. $r = new Response(-1, $duration, array(), null, curl_error($ch));
  160. curl_close($ch);
  161. return $r;
  162. }
  163. $code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
  164. $header_size = curl_getinfo($ch, CURLINFO_HEADER_SIZE);
  165. $headers = Header::parseRawText(substr($result, 0, $header_size));
  166. $body = substr($result, $header_size);
  167. curl_close($ch);
  168. return new Response($code, $duration, $headers, $body, null);
  169. }
  170. private static function escapeQuotes($str)
  171. {
  172. if (is_null($str)) {
  173. return null;
  174. }
  175. $find = array("\\", "\"");
  176. $replace = array("\\\\", "\\\"");
  177. return str_replace($find, $replace, $str);
  178. }
  179. }