You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

186 lines
6.0 KiB

  1. <?php
  2. namespace fast;
  3. /**
  4. * 字符串类
  5. */
  6. class Http
  7. {
  8. /**
  9. * 发送一个POST请求
  10. * @param string $url 请求URL
  11. * @param array $params 请求参数
  12. * @param array $options 扩展参数
  13. * @return mixed|string
  14. */
  15. public static function post($url, $params = [], $options = [])
  16. {
  17. $req = self::sendRequest($url, $params, 'POST', $options);
  18. return $req['ret'] ? $req['msg'] : '';
  19. }
  20. /**
  21. * 发送一个GET请求
  22. * @param string $url 请求URL
  23. * @param array $params 请求参数
  24. * @param array $options 扩展参数
  25. * @return mixed|string
  26. */
  27. public static function get($url, $params = [], $options = [])
  28. {
  29. $req = self::sendRequest($url, $params, 'GET', $options);
  30. return $req['ret'] ? $req['msg'] : '';
  31. }
  32. /**
  33. * CURL发送Request请求,含POST和REQUEST
  34. * @param string $url 请求的链接
  35. * @param mixed $params 传递的参数
  36. * @param string $method 请求的方法
  37. * @param mixed $options CURL的参数
  38. * @return array
  39. */
  40. public static function sendRequest($url, $params = [], $method = 'POST', $options = [])
  41. {
  42. $method = strtoupper($method);
  43. $protocol = substr($url, 0, 5);
  44. $query_string = is_array($params) ? http_build_query($params) : $params;
  45. $ch = curl_init();
  46. $defaults = [];
  47. if ('GET' == $method) {
  48. $geturl = $query_string ? $url . (stripos($url, "?") !== false ? "&" : "?") . $query_string : $url;
  49. $defaults[CURLOPT_URL] = $geturl;
  50. } else {
  51. $defaults[CURLOPT_URL] = $url;
  52. if ($method == 'POST') {
  53. $defaults[CURLOPT_POST] = 1;
  54. } else {
  55. $defaults[CURLOPT_CUSTOMREQUEST] = $method;
  56. }
  57. $defaults[CURLOPT_POSTFIELDS] = $params;
  58. }
  59. $defaults[CURLOPT_HEADER] = false;
  60. $defaults[CURLOPT_USERAGENT] = "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/45.0.2454.98 Safari/537.36";
  61. $defaults[CURLOPT_FOLLOWLOCATION] = true;
  62. $defaults[CURLOPT_RETURNTRANSFER] = true;
  63. $defaults[CURLOPT_CONNECTTIMEOUT] = 3;
  64. $defaults[CURLOPT_TIMEOUT] = 3;
  65. // disable 100-continue
  66. curl_setopt($ch, CURLOPT_HTTPHEADER, array('Expect:'));
  67. if ('https' == $protocol) {
  68. $defaults[CURLOPT_SSL_VERIFYPEER] = false;
  69. $defaults[CURLOPT_SSL_VERIFYHOST] = false;
  70. }
  71. curl_setopt_array($ch, (array)$options + $defaults);
  72. $ret = curl_exec($ch);
  73. $err = curl_error($ch);
  74. if (false === $ret || !empty($err)) {
  75. $errno = curl_errno($ch);
  76. $info = curl_getinfo($ch);
  77. curl_close($ch);
  78. return [
  79. 'ret' => false,
  80. 'errno' => $errno,
  81. 'msg' => $err,
  82. 'info' => $info,
  83. ];
  84. }
  85. curl_close($ch);
  86. return [
  87. 'ret' => true,
  88. 'msg' => $ret,
  89. ];
  90. }
  91. /**
  92. * 异步发送一个请求
  93. * @param string $url 请求的链接
  94. * @param mixed $params 请求的参数
  95. * @param string $method 请求的方法
  96. * @return boolean TRUE
  97. */
  98. public static function sendAsyncRequest($url, $params = [], $method = 'POST')
  99. {
  100. $method = strtoupper($method);
  101. $method = $method == 'POST' ? 'POST' : 'GET';
  102. //构造传递的参数
  103. if (is_array($params)) {
  104. $post_params = [];
  105. foreach ($params as $k => &$v) {
  106. if (is_array($v)) {
  107. $v = implode(',', $v);
  108. }
  109. $post_params[] = $k . '=' . urlencode($v);
  110. }
  111. $post_string = implode('&', $post_params);
  112. } else {
  113. $post_string = $params;
  114. }
  115. $parts = parse_url($url);
  116. //构造查询的参数
  117. if ($method == 'GET' && $post_string) {
  118. $parts['query'] = isset($parts['query']) ? $parts['query'] . '&' . $post_string : $post_string;
  119. $post_string = '';
  120. }
  121. $parts['query'] = isset($parts['query']) && $parts['query'] ? '?' . $parts['query'] : '';
  122. //发送socket请求,获得连接句柄
  123. $fp = fsockopen($parts['host'], isset($parts['port']) ? $parts['port'] : 80, $errno, $errstr, 3);
  124. if (!$fp) {
  125. return false;
  126. }
  127. //设置超时时间
  128. stream_set_timeout($fp, 3);
  129. $out = "{$method} {$parts['path']}{$parts['query']} HTTP/1.1\r\n";
  130. $out .= "Host: {$parts['host']}\r\n";
  131. $out .= "Content-Type: application/x-www-form-urlencoded\r\n";
  132. $out .= "Content-Length: " . strlen($post_string) . "\r\n";
  133. $out .= "Connection: Close\r\n\r\n";
  134. if ($post_string !== '') {
  135. $out .= $post_string;
  136. }
  137. fwrite($fp, $out);
  138. //不用关心服务器返回结果
  139. //echo fread($fp, 1024);
  140. fclose($fp);
  141. return true;
  142. }
  143. /**
  144. * 发送文件到客户端
  145. * @param string $file
  146. * @param bool $delaftersend
  147. * @param bool $exitaftersend
  148. */
  149. public static function sendToBrowser($file, $delaftersend = true, $exitaftersend = true)
  150. {
  151. if (file_exists($file) && is_readable($file)) {
  152. header('Content-Description: File Transfer');
  153. header('Content-Type: application/octet-stream');
  154. header('Content-Disposition: attachment;filename = ' . basename($file));
  155. header('Content-Transfer-Encoding: binary');
  156. header('Expires: 0');
  157. header('Cache-Control: must-revalidate, post-check = 0, pre-check = 0');
  158. header('Pragma: public');
  159. header('Content-Length: ' . filesize($file));
  160. ob_clean();
  161. flush();
  162. readfile($file);
  163. if ($delaftersend) {
  164. unlink($file);
  165. }
  166. if ($exitaftersend) {
  167. exit;
  168. }
  169. }
  170. }
  171. }