httpUtil.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: ssh <ssh@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. /**
  13. * 下载远程图片到本地 ssh 20220712
  14. */
  15. public static function downloadRemoteImg($sjId, $shopId, $url)
  16. {
  17. $ch = curl_init();
  18. curl_setopt($ch, CURLOPT_URL, $url);
  19. curl_setopt($ch, CURLOPT_HEADER, 0);
  20. curl_setopt($ch, CURLOPT_NOBODY, 0);//对body进行输出。
  21. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  22. $body = curl_exec($ch);
  23. curl_close($ch);
  24. $month = date("Ym");
  25. $date = date("d");
  26. $path = dirUtil::getImgUploadDir() . "/{$sjId}/{$shopId}/{$month}/{$date}/";
  27. if (!file_exists($path)) {
  28. //检查是否有该文件夹,如果没有就创建,并给予最高权限
  29. mkdir($path, 0700, true);
  30. }
  31. $preFileName = stringUtil::uniqueFileName();
  32. $newFile = $preFileName . ".jpg";
  33. $fullPathFile = $path . $newFile;
  34. $fp2 = @fopen($fullPathFile, 'a');
  35. fwrite($fp2, $body);
  36. fclose($fp2);
  37. $img = "uploads/{$sjId}/{$shopId}/{$month}/{$date}/" . $newFile;
  38. oss::uploadImage($img, $fullPathFile);
  39. return $img;
  40. }
  41. //专用方法,请勿修改 ssh 2019.11.21
  42. public static function getHttpHost()
  43. {
  44. $http = isset($_SERVER['REQUEST_SCHEME']) ? $_SERVER['REQUEST_SCHEME'] . '://' : 'https://';
  45. $suffix = 'huahb.com';
  46. if (isset($_SERVER['HTTP_HOST'])) {
  47. $hostInfo = $_SERVER['HTTP_HOST'];
  48. $parse = parse_url($hostInfo);
  49. $host = $parse['path'];
  50. $pos = strpos($host, '.') + 1;
  51. $suffix = substr($host, $pos);
  52. }
  53. return ['http' => $http, 'suffix' => $suffix];
  54. }
  55. //获得类似这样 http://m.hhb.com
  56. public static function getHost()
  57. {
  58. return Yii::$app->request->getHostInfo();
  59. }
  60. //获取header里的数据 ssh 2019.11.22
  61. public static function getHeader()
  62. {
  63. $ignore = ['host', 'accept', 'content-length', 'content-type'];
  64. $headers = [];
  65. foreach ($_SERVER as $key => $value) {
  66. if (substr($key, 0, 5) === 'HTTP_') {
  67. $key = substr($key, 5);
  68. $key = str_replace('_', ' ', $key);
  69. $key = str_replace(' ', '-', $key);
  70. $key = strtolower($key);
  71. if (!in_array($key, $ignore)) {
  72. $headers[$key] = $value;
  73. }
  74. }
  75. }
  76. //$token = isset($headers['token']) ? $headers['token'] : '';
  77. return $headers;
  78. }
  79. public static function getToken()
  80. {
  81. $header = self::getHeader();
  82. $token = isset($header['token']) ? $header['token'] : '';
  83. return $token;
  84. }
  85. //获取地址里的account ssh 2019.11.24
  86. public static function getAccount($url)
  87. {
  88. //前端链接,需要去掉链接#号前面部分进行解析
  89. $pos = strpos($url, '#');
  90. if (isset($pos)) {
  91. $url = substr($url, $pos + 1);
  92. }
  93. $parse = parse_url($url);
  94. $account = 0;
  95. if (isset($parse['query'])) {
  96. parse_str($parse['query'], $queryArray);
  97. if (isset($queryArray['account']) && !empty($queryArray['account'])) {
  98. $account = $queryArray['account'];
  99. }
  100. }
  101. return $account;
  102. }
  103. //获取平台id ssh 2019.11.28
  104. public static function getSourceId()
  105. {
  106. $ptSource = Yii::$app->params['ptSource'];
  107. $host = self::getHost();
  108. if (empty($host)) {
  109. return 0;
  110. }
  111. $arr = parse_url($host);
  112. if (isset($arr['host']) == false || empty($arr['host'])) {
  113. return 0;
  114. }
  115. $pos = strpos($arr['host'], '.');
  116. $ptName = substr($arr['host'], 0, $pos);
  117. return isset($ptSource[$ptName]) ? $ptSource[$ptName] : 0;
  118. }
  119. //判断是否微信浏览器
  120. public static function isWx()
  121. {
  122. if (isset($_SERVER['HTTP_USER_AGENT']) && strpos($_SERVER['HTTP_USER_AGENT'], 'MicroMessenger') == true) {
  123. return true;
  124. }
  125. return false;
  126. }
  127. //判断小程序 ssh 2019.12.13
  128. public static function isMiniProgram()
  129. {
  130. //根据头信息判断
  131. $userAgent = strtolower($_SERVER['HTTP_USER_AGENT']);
  132. if (strpos($userAgent, 'miniprogram') !== false) {
  133. return true;
  134. }
  135. //根据来源判断
  136. $referrer = Yii::$app->request->referrer;
  137. if (strpos($referrer, 'servicewechat.com') !== false) {
  138. return true;
  139. }
  140. return false;
  141. }
  142. //获取ip ssh 2019.12.18
  143. public static function ip()
  144. {
  145. return Yii::$app->request->userIP;
  146. }
  147. //变成https ssh 2020.2.20
  148. public static function becomeHttps($url)
  149. {
  150. $has = strpos($url, 'https');
  151. if ($has !== false) {
  152. return $url;
  153. }
  154. return str_replace('http', 'https', $url);
  155. }
  156. }