HttpService.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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. 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. $curl = curl_init($url);
  65. $method = strtoupper($method);
  66. //请求方式
  67. curl_setopt($curl, CURLOPT_CUSTOMREQUEST, $method);
  68. //post请求
  69. if ($method == 'POST') curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
  70. //超时时间
  71. curl_setopt($curl, CURLOPT_TIMEOUT, $timeout);
  72. //设置header头
  73. if ($header !== false) curl_setopt($curl, CURLOPT_HTTPHEADER, $header);
  74. curl_setopt($curl, CURLOPT_FAILONERROR, false);
  75. //返回抓取数据
  76. curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
  77. //输出header头信息
  78. curl_setopt($curl, CURLOPT_HEADER, true);
  79. //TRUE 时追踪句柄的请求字符串,从 PHP 5.1.3 开始可用。这个很关键,就是允许你查看请求header
  80. curl_setopt($curl, CURLINFO_HEADER_OUT, true);
  81. //https请求
  82. if (1 == strpos("$" . $url, "https://")) {
  83. curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
  84. curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
  85. }
  86. self::$curlError = curl_error($curl);
  87. list($content, $status) = [curl_exec($curl), curl_getinfo($curl), curl_close($curl)];
  88. self::$status = $status;
  89. self::$headerStr = trim(substr($content, 0, $status['header_size']));
  90. $content = trim(substr($content, $status['header_size']));
  91. return (intval($status["http_code"]) === 200) ? $content : false;
  92. }
  93. /**
  94. * 模拟POST发起请求
  95. * @param $url
  96. * @param array $data
  97. * @param bool $header
  98. * @param int $timeout
  99. * @return bool|string
  100. */
  101. public static function postRequest($url, array $data = array(), $header = false, $timeout = 10)
  102. {
  103. return self::request($url, 'post', $data, $header, $timeout);
  104. }
  105. /**
  106. * 获取header头字符串类型
  107. * @return mixed
  108. */
  109. public static function getHeaderStr()
  110. {
  111. return self::$headerStr;
  112. }
  113. /**
  114. * 获取header头数组类型
  115. * @return array
  116. */
  117. public static function getHeader()
  118. {
  119. $headArr = explode("\r\n", self::$headerStr);
  120. return $headArr;
  121. }
  122. }