HttpService.php 3.5 KB

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