httpUtil.php 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  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 getHostData()
  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. //获取地址里的account shish 2019.11.24
  51. public static function getAccount($url)
  52. {
  53. //前端链接,需要去掉链接#号前面部分进行解析
  54. $pos = strpos($url, '#');
  55. if (isset($pos)) {
  56. $url = substr($url, $pos + 1);
  57. }
  58. $parse = parse_url($url);
  59. $account = 0;
  60. if (isset($parse['query'])) {
  61. parse_str($parse['query'], $queryArray);
  62. if (isset($queryArray['account']) && !empty($queryArray['account'])) {
  63. $account = $queryArray['account'];
  64. }
  65. }
  66. return $account;
  67. }
  68. }