httpUtil.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: shish <shish@zhhinc.com>
  5. * Date: 2019/11/22 0022
  6. * Time: 9:46
  7. */
  8. namespace common\components;
  9. use Yii;
  10. class httpUtil
  11. {
  12. //专用方法,请勿修改 shish 2019.11.21
  13. public static function getHttpHost()
  14. {
  15. $http = isset($_SERVER['REQUEST_SCHEME']) ? $_SERVER['REQUEST_SCHEME'] . '://' : 'https://';
  16. $suffix = 'huahb.com';
  17. if (isset($_SERVER['HTTP_HOST'])) {
  18. $hostInfo = $_SERVER['HTTP_HOST'];
  19. $parse = parse_url($hostInfo);
  20. $host = $parse['path'];
  21. $pos = strpos($host, '.') + 1;
  22. $suffix = substr($host, $pos);
  23. }
  24. return ['http' => $http, 'suffix' => $suffix];
  25. }
  26. //获得类似这样 http://m.hhb.com
  27. public static function getHost()
  28. {
  29. return Yii::$app->request->getHostInfo();
  30. }
  31. //获取header里的数据 shish 2019.11.22
  32. public static function getHeader()
  33. {
  34. $ignore = ['host', 'accept', 'content-length', 'content-type'];
  35. $headers = [];
  36. foreach ($_SERVER as $key => $value) {
  37. if (substr($key, 0, 5) === 'HTTP_') {
  38. $key = substr($key, 5);
  39. $key = str_replace('_', ' ', $key);
  40. $key = str_replace(' ', '-', $key);
  41. $key = strtolower($key);
  42. if (!in_array($key, $ignore)) {
  43. $headers[$key] = $value;
  44. }
  45. }
  46. }
  47. //$token = isset($headers['token']) ? $headers['token'] : '';
  48. return $headers;
  49. }
  50. public static function getToken()
  51. {
  52. $header = self::getHeader();
  53. $token = isset($header['token']) ? $header['token'] : '';
  54. return $token;
  55. }
  56. //获取地址里的account shish 2019.11.24
  57. public static function getAccount($url)
  58. {
  59. //前端链接,需要去掉链接#号前面部分进行解析
  60. $pos = strpos($url, '#');
  61. if (isset($pos)) {
  62. $url = substr($url, $pos + 1);
  63. }
  64. $parse = parse_url($url);
  65. $account = 0;
  66. if (isset($parse['query'])) {
  67. parse_str($parse['query'], $queryArray);
  68. if (isset($queryArray['account']) && !empty($queryArray['account'])) {
  69. $account = $queryArray['account'];
  70. }
  71. }
  72. return $account;
  73. }
  74. //获取平台id shish 2019.11.28
  75. public static function getSourceId()
  76. {
  77. $ptSource = Yii::$app->params['ptSource'];
  78. $host = self::getHost();
  79. if (empty($host)) {
  80. return 0;
  81. }
  82. $arr = parse_url($host);
  83. if (isset($arr['host']) == false || empty($arr['host'])) {
  84. return 0;
  85. }
  86. $pos = strpos($arr['host'], '.');
  87. $ptName = substr($arr['host'], 0, $pos);
  88. return isset($ptSource[$ptName]) ? $ptSource[$ptName] : 0;
  89. }
  90. //判断是否微信浏览器
  91. public static function isWx()
  92. {
  93. if (isset($_SERVER['HTTP_USER_AGENT']) && strpos($_SERVER['HTTP_USER_AGENT'], 'MicroMessenger') == true) {
  94. return true;
  95. }
  96. return false;
  97. }
  98. //判断小程序 shish 2019.12.13
  99. public static function isMiniProgram()
  100. {
  101. //根据头信息判断
  102. $userAgent = strtolower($_SERVER['HTTP_USER_AGENT']);
  103. if (strpos($userAgent, 'miniprogram') !== false) {
  104. return true;
  105. }
  106. //根据来源判断
  107. $referrer = Yii::$app->request->referrer;
  108. if (strpos($referrer, 'servicewechat.com') !== false) {
  109. return true;
  110. }
  111. return false;
  112. }
  113. //获取ip shish 2019.12.18
  114. public static function ip()
  115. {
  116. return Yii::$app->request->userIP;
  117. }
  118. //变成https shish 2020.2.20
  119. public static function becomeHttps($url)
  120. {
  121. $has = strpos($url, 'https');
  122. if ($has !== false) {
  123. return $url;
  124. }
  125. return str_replace('http', 'https', $url);
  126. }
  127. }