HttpService.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2020 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. //post请求
  82. if ($method == 'POST') curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
  83. //超时时间
  84. curl_setopt($curl, CURLOPT_TIMEOUT, $timeout);
  85. //设置header头
  86. if ($header !== false) curl_setopt($curl, CURLOPT_HTTPHEADER, $header);
  87. curl_setopt($curl, CURLOPT_FAILONERROR, false);
  88. //返回抓取数据
  89. curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
  90. //输出header头信息
  91. curl_setopt($curl, CURLOPT_HEADER, true);
  92. //TRUE 时追踪句柄的请求字符串,从 PHP 5.1.3 开始可用。这个很关键,就是允许你查看请求header
  93. curl_setopt($curl, CURLINFO_HEADER_OUT, true);
  94. //https请求
  95. if (1 == strpos("$" . $url, "https://")) {
  96. curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
  97. curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
  98. }
  99. self::$curlError = curl_error($curl);
  100. [$content, $status] = [curl_exec($curl), curl_getinfo($curl), curl_close($curl)];
  101. self::$status = $status;
  102. self::$headerStr = trim(substr($content, 0, $status['header_size']));
  103. $content = trim(substr($content, $status['header_size']));
  104. return (intval($status["http_code"]) === 200) ? $content : false;
  105. }
  106. /**
  107. * 模拟POST发起请求
  108. * @param $url
  109. * @param array $data
  110. * @param bool $header
  111. * @param int $timeout
  112. * @return bool|string
  113. */
  114. public static function postRequest($url, $data = [], $header = false, $timeout = 10)
  115. {
  116. return self::request($url, 'post', $data, $header, $timeout);
  117. }
  118. /**
  119. * 获取header头字符串类型
  120. * @return mixed
  121. */
  122. public static function getHeaderStr()
  123. {
  124. return self::$headerStr;
  125. }
  126. /**
  127. * 获取header头数组类型
  128. * @return array
  129. */
  130. public static function getHeader()
  131. {
  132. $headArr = explode("\r\n", self::$headerStr);
  133. return $headArr;
  134. }
  135. }