httpUtil.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  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. $token = $_SERVER['HTTP_TOKEN'] ?? '';
  82. if (empty($token)) {
  83. $token = Yii::$app->request->get('token', '');
  84. }
  85. return $token;
  86. }
  87. //获取地址里的account ssh 2019.11.24
  88. public static function getAccount($url)
  89. {
  90. //前端链接,需要去掉链接#号前面部分进行解析
  91. $pos = strpos($url, '#');
  92. if (isset($pos)) {
  93. $url = substr($url, $pos + 1);
  94. }
  95. $parse = parse_url($url);
  96. $account = 0;
  97. if (isset($parse['query'])) {
  98. parse_str($parse['query'], $queryArray);
  99. if (isset($queryArray['account']) && !empty($queryArray['account'])) {
  100. $account = $queryArray['account'];
  101. }
  102. }
  103. return $account;
  104. }
  105. //获取平台id ssh 2019.11.28
  106. public static function getSourceId()
  107. {
  108. $ptSource = Yii::$app->params['ptSource'];
  109. $host = self::getHost();
  110. if (empty($host)) {
  111. return 0;
  112. }
  113. $arr = parse_url($host);
  114. if (isset($arr['host']) == false || empty($arr['host'])) {
  115. return 0;
  116. }
  117. $pos = strpos($arr['host'], '.');
  118. $ptName = substr($arr['host'], 0, $pos);
  119. return isset($ptSource[$ptName]) ? $ptSource[$ptName] : 0;
  120. }
  121. //判断是否微信浏览器
  122. public static function isWx()
  123. {
  124. if (isset($_SERVER['HTTP_USER_AGENT']) && strpos($_SERVER['HTTP_USER_AGENT'], 'MicroMessenger') == true) {
  125. return true;
  126. }
  127. return false;
  128. }
  129. //判断小程序 ssh 2019.12.13
  130. public static function isMiniProgram()
  131. {
  132. //根据头信息判断
  133. $userAgent = strtolower($_SERVER['HTTP_USER_AGENT']);
  134. if (strpos($userAgent, 'miniprogram') !== false) {
  135. return true;
  136. }
  137. //根据来源判断
  138. $referrer = Yii::$app->request->referrer;
  139. if (strpos($referrer, 'servicewechat.com') !== false) {
  140. return true;
  141. }
  142. return false;
  143. }
  144. //获取ip ssh 2019.12.18
  145. public static function ip()
  146. {
  147. return Yii::$app->request->userIP;
  148. }
  149. //变成https ssh 2020.2.20
  150. public static function becomeHttps($url)
  151. {
  152. $has = strpos($url, 'https');
  153. if ($has !== false) {
  154. return $url;
  155. }
  156. return str_replace('http', 'https', $url);
  157. }
  158. }