HttpService.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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 service;
  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. public static function getStatus()
  28. {
  29. return self::$status;
  30. }
  31. public static function getRequest($url, $data = array(), $header = false, $timeout = 10)
  32. {
  33. if (!empty($data)) {
  34. $url .= (stripos($url, '?') === false ? '?' : '&');
  35. $url .= (is_array($data) ? http_build_query($data) : $data);
  36. }
  37. return self::request($url, 'get', array(), $header, $timeout);
  38. }
  39. public static function request($url, $method = 'get', $data = array(), $header = false, $timeout = 15)
  40. {
  41. self::$status = null;
  42. self::$curlError = null;
  43. self::$headerStr = null;
  44. $curl = curl_init($url);
  45. $method = strtoupper($method);
  46. //请求方式
  47. curl_setopt($curl, CURLOPT_CUSTOMREQUEST, $method);
  48. //post请求
  49. if ($method == 'POST') curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
  50. //超时时间
  51. curl_setopt($curl, CURLOPT_TIMEOUT, $timeout);
  52. //设置header头
  53. if ($header !== false) curl_setopt($curl, CURLOPT_HTTPHEADER, $header);
  54. curl_setopt($curl, CURLOPT_FAILONERROR, false);
  55. //返回抓取数据
  56. curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
  57. //输出header头信息
  58. curl_setopt($curl, CURLOPT_HEADER, true);
  59. //TRUE 时追踪句柄的请求字符串,从 PHP 5.1.3 开始可用。这个很关键,就是允许你查看请求header
  60. curl_setopt($curl, CURLINFO_HEADER_OUT, true);
  61. //https请求
  62. if (1 == strpos("$" . $url, "https://")) {
  63. curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
  64. curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
  65. }
  66. self::$curlError = curl_error($curl);
  67. list($content, $status) = [curl_exec($curl), curl_getinfo($curl), curl_close($curl)];
  68. self::$status = $status;
  69. self::$headerStr = trim(substr($content, 0, $status['header_size']));
  70. $content = trim(substr($content, $status['header_size']));
  71. return (intval($status["http_code"]) === 200) ? $content : false;
  72. }
  73. public static function postRequest($url, $data = array(), $header = false, $timeout = 10)
  74. {
  75. return self::request($url, 'post', $data, $header, $timeout);
  76. }
  77. public static function getHeaderStr()
  78. {
  79. return self::$headerStr;
  80. }
  81. public static function getHeader()
  82. {
  83. $headArr = explode("\r\n", self::$headerStr);
  84. return $headArr;
  85. }
  86. }