HttpService.php 4.2 KB

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