Client.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. <?php
  2. namespace Qiniu\Http;
  3. use Qiniu\Config;
  4. use Qiniu\Http\Request;
  5. use Qiniu\Http\Response;
  6. final class Client
  7. {
  8. public static function get($url, array $headers = array())
  9. {
  10. $request = new Request('GET', $url, $headers);
  11. return self::sendRequest($request);
  12. }
  13. public static function delete($url, array $headers = array())
  14. {
  15. $request = new Request('DELETE', $url, $headers);
  16. return self::sendRequest($request);
  17. }
  18. public static function post($url, $body, array $headers = array())
  19. {
  20. $request = new Request('POST', $url, $headers, $body);
  21. return self::sendRequest($request);
  22. }
  23. public static function PUT($url, $body, array $headers = array())
  24. {
  25. $request = new Request('PUT', $url, $headers, $body);
  26. return self::sendRequest($request);
  27. }
  28. public static function multipartPost(
  29. $url,
  30. $fields,
  31. $name,
  32. $fileName,
  33. $fileBody,
  34. $mimeType = null,
  35. array $headers = array()
  36. ) {
  37. $data = array();
  38. $mimeBoundary = md5(microtime());
  39. foreach ($fields as $key => $val) {
  40. array_push($data, '--' . $mimeBoundary);
  41. array_push($data, "Content-Disposition: form-data; name=\"$key\"");
  42. array_push($data, '');
  43. array_push($data, $val);
  44. }
  45. array_push($data, '--' . $mimeBoundary);
  46. $finalMimeType = empty($mimeType) ? 'application/octet-stream' : $mimeType;
  47. $finalFileName = self::escapeQuotes($fileName);
  48. array_push($data, "Content-Disposition: form-data; name=\"$name\"; filename=\"$finalFileName\"");
  49. array_push($data, "Content-Type: $finalMimeType");
  50. array_push($data, '');
  51. array_push($data, $fileBody);
  52. array_push($data, '--' . $mimeBoundary . '--');
  53. array_push($data, '');
  54. $body = implode("\r\n", $data);
  55. // var_dump($data);exit;
  56. $contentType = 'multipart/form-data; boundary=' . $mimeBoundary;
  57. $headers['Content-Type'] = $contentType;
  58. $request = new Request('POST', $url, $headers, $body);
  59. return self::sendRequest($request);
  60. }
  61. private static function userAgent()
  62. {
  63. $sdkInfo = "QiniuPHP/" . Config::SDK_VER;
  64. $systemInfo = php_uname("s");
  65. $machineInfo = php_uname("m");
  66. $envInfo = "($systemInfo/$machineInfo)";
  67. $phpVer = phpversion();
  68. $ua = "$sdkInfo $envInfo PHP/$phpVer";
  69. return $ua;
  70. }
  71. public static function sendRequest($request)
  72. {
  73. $t1 = microtime(true);
  74. $ch = curl_init();
  75. $options = array(
  76. CURLOPT_USERAGENT => self::userAgent(),
  77. CURLOPT_RETURNTRANSFER => true,
  78. CURLOPT_SSL_VERIFYPEER => false,
  79. CURLOPT_SSL_VERIFYHOST => false,
  80. CURLOPT_HEADER => true,
  81. CURLOPT_NOBODY => false,
  82. CURLOPT_CUSTOMREQUEST => $request->method,
  83. CURLOPT_URL => $request->url,
  84. );
  85. // Handle open_basedir & safe mode
  86. if (!ini_get('safe_mode') && !ini_get('open_basedir')) {
  87. $options[CURLOPT_FOLLOWLOCATION] = true;
  88. }
  89. if (!empty($request->headers)) {
  90. $headers = array();
  91. foreach ($request->headers as $key => $val) {
  92. array_push($headers, "$key: $val");
  93. }
  94. $options[CURLOPT_HTTPHEADER] = $headers;
  95. }
  96. curl_setopt($ch, CURLOPT_HTTPHEADER, array('Expect:'));
  97. if (!empty($request->body)) {
  98. $options[CURLOPT_POSTFIELDS] = $request->body;
  99. }
  100. curl_setopt_array($ch, $options);
  101. $result = curl_exec($ch);
  102. $t2 = microtime(true);
  103. $duration = round($t2 - $t1, 3);
  104. $ret = curl_errno($ch);
  105. if ($ret !== 0) {
  106. $r = new Response(-1, $duration, array(), null, curl_error($ch));
  107. curl_close($ch);
  108. return $r;
  109. }
  110. $code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
  111. $header_size = curl_getinfo($ch, CURLINFO_HEADER_SIZE);
  112. $headers = self::parseHeaders(substr($result, 0, $header_size));
  113. $body = substr($result, $header_size);
  114. curl_close($ch);
  115. return new Response($code, $duration, $headers, $body, null);
  116. }
  117. private static function parseHeaders($raw)
  118. {
  119. $headers = array();
  120. $headerLines = explode("\r\n", $raw);
  121. foreach ($headerLines as $line) {
  122. $headerLine = trim($line);
  123. $kv = explode(':', $headerLine);
  124. if (count($kv) > 1) {
  125. $kv[0] =self::ucwordsHyphen($kv[0]);
  126. $headers[$kv[0]] = trim($kv[1]);
  127. }
  128. }
  129. return $headers;
  130. }
  131. private static function escapeQuotes($str)
  132. {
  133. $find = array("\\", "\"");
  134. $replace = array("\\\\", "\\\"");
  135. return str_replace($find, $replace, $str);
  136. }
  137. private static function ucwordsHyphen($str)
  138. {
  139. return str_replace('- ', '-', ucwords(str_replace('-', '- ', $str)));
  140. }
  141. }