HttpService.php 4.4 KB

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