Client.php 4.4 KB

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