Http.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. <?php
  2. namespace app\extra\basic;
  3. class Http
  4. {
  5. /**
  6. * 错误信息
  7. * @var string
  8. */
  9. private static $curlError;
  10. /**
  11. * header头信息
  12. * @var string
  13. */
  14. private static $headerStr;
  15. /**
  16. * 请求状态
  17. * @var int
  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 $data
  97. * @param bool $header
  98. * @param int $timeout
  99. * @return bool|string
  100. */
  101. public static function postRequest($url, $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. }