Request.php 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  1. <?php
  2. /**
  3. * Request.php UTF-8
  4. * 各个请求函数
  5. *
  6. * @date : 2016年11月16日下午2:45:36
  7. *
  8. * @license 这不是一个自由软件,未经授权不许任何使用和传播。
  9. * @author : wuyonghong <wyh@huosdk.com>
  10. * @version : HUOSDK 7.0
  11. * @modified: 2016年11月16日下午2:45:36
  12. */
  13. namespace huolib\oa;
  14. use think\Log;
  15. if (!function_exists('daddslashes')) {
  16. function daddslashes($string, $force = 0) {
  17. return uc_addslashes($string, $force);
  18. }
  19. }
  20. if (!function_exists('dhtmlspecialchars')) {
  21. function dhtmlspecialchars($string, $flags = null) {
  22. if (is_array($string)) {
  23. foreach ($string as $key => $val) {
  24. $string[$key] = dhtmlspecialchars($val, $flags);
  25. }
  26. } else {
  27. if ($flags === null) {
  28. $string = str_replace(array('&', '"', '<', '>'), array('&amp;', '&quot;', '&lt;', '&gt;'), $string);
  29. if (strpos($string, '&amp;#') !== false) {
  30. $string = preg_replace('/&amp;((#(\d{3,5}|x[a-fA-F0-9]{4}));)/', '&\\1', $string);
  31. }
  32. } else {
  33. if (PHP_VERSION < '5.4.0') {
  34. $string = htmlspecialchars($string, $flags);
  35. } else {
  36. if (strtolower(CHARSET) == 'utf-8') {
  37. $charset = 'UTF-8';
  38. } else {
  39. $charset = 'ISO-8859-1';
  40. }
  41. $string = htmlspecialchars($string, $flags, $charset);
  42. }
  43. }
  44. }
  45. return $string;
  46. }
  47. }
  48. if (!function_exists('fsocketopen')) {
  49. function fsocketopen($hostname, $port = 80, &$errno, &$errstr, $timeout = 15) {
  50. $fp = '';
  51. if (function_exists('fsockopen')) {
  52. $fp = @fsockopen($hostname, $port, $errno, $errstr, $timeout);
  53. } elseif (function_exists('pfsockopen')) {
  54. $fp = @pfsockopen($hostname, $port, $errno, $errstr, $timeout);
  55. } elseif (function_exists('stream_socket_client')) {
  56. $fp = @stream_socket_client($hostname.':'.$port, $errno, $errstr, $timeout);
  57. }
  58. return $fp;
  59. }
  60. }
  61. class Request {
  62. /**
  63. * 自定义错误处理
  64. *
  65. * @param 输出的文件 $msg
  66. * @param string $level
  67. *
  68. * @internal param 输出的文件 $msg
  69. */
  70. private function _error($msg, $level = 'error') {
  71. $_info = 'request\Request Error:'.$msg;
  72. Log::record($_info, $level);
  73. }
  74. /**
  75. * 异步请求
  76. *
  77. * @param $url string 请求地址与端口
  78. * @param $post string post数据
  79. * @param $cookie string cookie数据
  80. * @param $timeout int 请求超时时间
  81. *
  82. * @return 请求结果
  83. */
  84. public static function asyncRequst($url, $post, $cookie, $timeout) {
  85. return self::socketRequest($url, 0, $post, $cookie, true, '', $timeout, false);
  86. }
  87. /**
  88. * socket 请求函数
  89. *
  90. * @param $url string 请求地址与端口
  91. * @param $limit int 读取长度
  92. * @param $post string post数据
  93. * @param $cookie string cookie数据
  94. * @param $bysocket bool 是否使用socket
  95. * @param bool|string $ip bool 请求连接的IP 空则取$host
  96. * @param $timeout int 请求超时时间
  97. * @param $block bool 是否阻塞
  98. * @param $encodetype string 是否启用urldecode
  99. *
  100. * @return 请求结果
  101. */
  102. public static function socketRequest(
  103. $url, $limit = 0, $post = '', $cookie = '', $bysocket = false, $ip = '', $timeout = 15, $block = true,
  104. $encodetype = 'URLENCODE'
  105. ) {
  106. $return = '';
  107. $matches = parse_url($url);
  108. $scheme = $matches['scheme'];
  109. $host = $matches['host'];
  110. $path = $matches['path'] ? $matches['path'].((isset($matches['query'])&&$matches['query']) ? '?'.$matches['query'] : '') : '/';
  111. $port = !empty($matches['port']) ? $matches['port'] : 80;
  112. if ($post) {
  113. $out = "POST $path HTTP/1.0\r\n";
  114. $header = "Accept: */*\r\n";
  115. $header .= "Accept-Language: zh-cn\r\n";
  116. $boundary = $encodetype == 'URLENCODE' ? '' : ';'.substr($post, 0, trim(strpos($post, "\n")));
  117. $header .= $encodetype == 'URLENCODE' ? "Content-Type: application/x-www-form-urlencoded\r\n"
  118. : "Content-Type: multipart/form-data$boundary\r\n";
  119. $header .= "User-Agent: $_SERVER[HTTP_USER_AGENT]\r\n";
  120. $header .= "Host: $host:$port\r\n";
  121. $header .= 'Content-Length: '.strlen($post)."\r\n";
  122. $header .= "Connection: Close\r\n";
  123. $header .= "Cache-Control: no-cache\r\n";
  124. $header .= "Cookie: $cookie\r\n\r\n";
  125. $out .= $header.$post;
  126. } else {
  127. $out = "GET $path HTTP/1.0\r\n";
  128. $header = "Accept: */*\r\n";
  129. $header .= "Accept-Language: zh-cn\r\n";
  130. $header .= "User-Agent: $_SERVER[HTTP_USER_AGENT]\r\n";
  131. $header .= "Host: $host:$port\r\n";
  132. $header .= "Connection: Close\r\n";
  133. $header .= "Cookie: $cookie\r\n\r\n";
  134. $out .= $header;
  135. }
  136. $fpflag = 0;
  137. if (!$bysocket || !$fp = @fsocketopen(($ip ? $ip : $host), $port, $errno, $errstr, $timeout)) {
  138. $context = array('http' => array('method' => $post ? 'POST' : 'GET', 'header' => $header,
  139. 'content' => $post, 'timeout' => $timeout
  140. )
  141. );
  142. $context = stream_context_create($context);
  143. $fp = @fopen($scheme.'://'.($ip ? $ip : $host).':'.$port.$path, 'b', false, $context);
  144. $fpflag = 1;
  145. }
  146. if (!$fp) {
  147. return '';
  148. } else {
  149. stream_set_blocking($fp, $block);
  150. stream_set_timeout($fp, $timeout);
  151. @fwrite($fp, $out);
  152. $status = stream_get_meta_data($fp);
  153. if (!$status['timed_out']) {
  154. while (!feof($fp) && !$fpflag) {
  155. if (($header = @fgets($fp)) && ($header == "\r\n" || $header == "\n")) {
  156. break;
  157. }
  158. }
  159. $stop = false;
  160. while (!feof($fp) && !$stop) {
  161. $data = fread($fp, ($limit == 0 || $limit > 8192 ? 8192 : $limit));
  162. $return .= $data;
  163. if ($limit) {
  164. $limit -= strlen($data);
  165. $stop = $limit <= 0;
  166. }
  167. }
  168. }
  169. @fclose($fp);
  170. return $return;
  171. }
  172. }
  173. /**
  174. * CP 回调请求函数
  175. *
  176. * @param $url string 请求地址与端口
  177. * @param $params string post数据
  178. *
  179. * @return 请求结果
  180. */
  181. public static function callBack($url, $params) {
  182. $curl = curl_init(); //初始化curl
  183. curl_setopt($curl, CURLOPT_URL, $url);
  184. curl_setopt($curl, CURLOPT_HEADER, 0); // 过滤HTTP头
  185. curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);// 显示输出结果
  186. curl_setopt($curl, CURLOPT_POST, 1); // post传输数据
  187. curl_setopt($curl, CURLOPT_POSTFIELDS, $params);// post传输数据
  188. curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, 3);//设置等待时间
  189. //https 请求
  190. if (strlen($url) > 5 && strtolower(substr($url, 0, 5)) == "https") {
  191. curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
  192. curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
  193. }
  194. $header = array("Content-Type: application/x-www-form-urlencoded; charset=UTF-8");
  195. curl_setopt($curl, CURLOPT_HTTPHEADER, $header);
  196. //https 请求
  197. if (strlen($url) > 5 && strtolower(substr($url, 0, 5)) == "https") {
  198. curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
  199. curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
  200. }
  201. $responseText = curl_exec($curl);
  202. curl_close($curl);
  203. $rs = strtoupper($responseText);
  204. $result = 0;
  205. if ('SUCCESS' == $rs) {
  206. $result = 1;
  207. } else {
  208. $result = 0;
  209. }
  210. return $result;
  211. }
  212. /**
  213. * CP 回调请求函数
  214. *
  215. * @param $url string 请求地址与端口
  216. * @param $params string post数据
  217. *
  218. * @return 请求结果
  219. */
  220. public static function httpJsonpost($url, $params) {
  221. $ch = curl_init();
  222. curl_setopt($ch, CURLOPT_POST, 1);
  223. curl_setopt($ch, CURLOPT_URL, $url);
  224. curl_setopt($ch, CURLOPT_POSTFIELDS, $params);
  225. curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 3);//设置等待时间
  226. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);//要求结果为字符串且输出到屏幕上
  227. //https 请求
  228. if (strlen($url) > 5 && strtolower(substr($url, 0, 5)) == "https") {
  229. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
  230. curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
  231. }
  232. curl_setopt(
  233. $ch, CURLOPT_HTTPHEADER, array(
  234. 'Content-Type: application/json; charset=utf-8',
  235. 'Content-Length: '.strlen($params))
  236. );
  237. //ob_start();
  238. $return_content = curl_exec($ch);
  239. //$return_content = ob_get_contents();
  240. //ob_end_clean();
  241. return $return_content;
  242. }
  243. }