HttpService.php 4.0 KB

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