HttpService.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. <?php
  2. namespace liuniu;
  3. class HttpService
  4. {
  5. /**
  6. * 错误信息
  7. * @var string
  8. */
  9. private static $curlError;
  10. /**
  11. * header头信息
  12. * @var string
  13. */
  14. private static $headerStr;
  15. /**
  16. * 请求状态
  17. * @var int
  18. */
  19. private static $status;
  20. /**
  21. * 获取请求错误信息
  22. * @return string
  23. */
  24. public static function getCurlError()
  25. {
  26. return self::$curlError;
  27. }
  28. /**
  29. * 获取请求响应状态
  30. * @return mixed
  31. */
  32. public static function getStatus()
  33. {
  34. return self::$status;
  35. }
  36. /**
  37. * 模拟GET发起请求
  38. * @param $url 请求地址
  39. * @param array $data 请求数据
  40. * @param bool $header header头
  41. * @param int $timeout 响应超时时间
  42. * @return bool|string
  43. */
  44. public static function getRequest($url, $data = array(), $header = false, $timeout = 10)
  45. {
  46. if (!empty($data)) {
  47. $url .= (stripos($url, '?') === false ? '?' : '&');
  48. $url .= (is_array($data) ? http_build_query($data) : $data);
  49. }
  50. return self::request($url, 'get', array(), $header, $timeout);
  51. }
  52. /**
  53. * curl 请求
  54. * @param $url 请求地址
  55. * @param string $method 请求方式
  56. * @param array $data 请求数据
  57. * @param bool $header 请求header头
  58. * @param int $timeout 超时秒数
  59. * @return bool|string
  60. */
  61. public static function request($url, $method = 'get', $data = array(), $header = false, $timeout = 15,$json=false)
  62. {
  63. self::$status = null;
  64. self::$curlError = null;
  65. self::$headerStr = null;
  66. $curl = curl_init($url);
  67. $method = strtoupper($method);
  68. //请求方式
  69. curl_setopt($curl, CURLOPT_CUSTOMREQUEST, $method);
  70. //post请求
  71. if($json && is_array($data))
  72. {
  73. $data = json_encode($data);
  74. }
  75. if ($method == 'POST')
  76. {
  77. curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
  78. }
  79. //超时时间
  80. curl_setopt($curl, CURLOPT_TIMEOUT, $timeout);
  81. //设置header头
  82. if ($header !== false) curl_setopt($curl, CURLOPT_HTTPHEADER, $header);
  83. curl_setopt($curl, CURLOPT_FAILONERROR, false);
  84. //返回抓取数据
  85. curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
  86. //输出header头信息
  87. if($json){ //发送JSON数据
  88. curl_setopt($curl, CURLOPT_HEADER, 0);
  89. curl_setopt($curl, CURLOPT_HTTPHEADER,array('Content-Type: application/json; charset=utf-8','Content-Length:' . strlen($data)));
  90. }
  91. else {
  92. curl_setopt($curl, CURLOPT_HEADER, true);
  93. }
  94. //TRUE 时追踪句柄的请求字符串,从 PHP 5.1.3 开始可用。这个很关键,就是允许你查看请求header
  95. curl_setopt($curl, CURLINFO_HEADER_OUT, true);
  96. //https请求
  97. if (1 == strpos("$" . $url, "https://")) {
  98. curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
  99. curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
  100. }
  101. self::$curlError = curl_error($curl);
  102. list($content, $status) = [curl_exec($curl), curl_getinfo($curl), curl_close($curl)];
  103. self::$status = $status;
  104. self::$headerStr = trim(substr($content, 0, $status['header_size']));
  105. $content = trim(substr($content, $status['header_size']));
  106. return (intval($status["http_code"]) === 200) ? $content : false;
  107. }
  108. /**
  109. * 模拟POST发起请求
  110. * @param $url 请求链接
  111. * @param array $data 请求参数
  112. * @param bool $header header头
  113. * @param int $timeout 超时秒数
  114. * @return bool|string
  115. */
  116. public static function postRequest($url, array $data = array(), $header = false, $timeout = 10,$json=false)
  117. {
  118. return self::request($url, 'post', $data, $header, $timeout,$json);
  119. }
  120. /**
  121. * 获取header头字符串类型
  122. * @return mixed
  123. */
  124. public static function getHeaderStr(): string
  125. {
  126. return self::$headerStr;
  127. }
  128. /**
  129. * 获取header头数组类型
  130. * @return array
  131. */
  132. public static function getHeader(): array
  133. {
  134. $headArr = explode("\r\n", self::$headerStr);
  135. return $headArr;
  136. }
  137. }