HttpService.php 3.5 KB

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