Sin descripción
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.

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