httpUtil.php 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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. 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. }