| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175 |
- <?php
- /**
- * Created by PhpStorm.
- * User: ssh <ssh@zhhinc.com>
- * Date: 2019/11/22 0022
- * Time: 9:46
- */
- namespace common\components;
- use Yii;
- class httpUtil
- {
- /**
- * 下载远程图片到本地 ssh 20220712
- */
- public static function downloadRemoteImg($sjId, $shopId, $url)
- {
- $ch = curl_init();
- curl_setopt($ch, CURLOPT_URL, $url);
- curl_setopt($ch, CURLOPT_HEADER, 0);
- curl_setopt($ch, CURLOPT_NOBODY, 0);//对body进行输出。
- curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
- $body = curl_exec($ch);
- curl_close($ch);
- $month = date("Ym");
- $date = date("d");
- $path = dirUtil::getImgUploadDir() . "/{$sjId}/{$shopId}/{$month}/{$date}/";
- if (!file_exists($path)) {
- //检查是否有该文件夹,如果没有就创建,并给予最高权限
- mkdir($path, 0700, true);
- }
- $preFileName = stringUtil::uniqueFileName();
- $newFile = $preFileName . ".jpg";
- $fullPathFile = $path . $newFile;
- $fp2 = @fopen($fullPathFile, 'a');
- fwrite($fp2, $body);
- fclose($fp2);
- $img = "uploads/{$sjId}/{$shopId}/{$month}/{$date}/" . $newFile;
- oss::uploadImage($img, $fullPathFile);
- return $img;
- }
- //专用方法,请勿修改 ssh 2019.11.21
- public static function getHttpHost()
- {
- $http = isset($_SERVER['REQUEST_SCHEME']) ? $_SERVER['REQUEST_SCHEME'] . '://' : 'https://';
- $suffix = 'huahb.com';
- if (isset($_SERVER['HTTP_HOST'])) {
- $hostInfo = $_SERVER['HTTP_HOST'];
- $parse = parse_url($hostInfo);
- $host = $parse['path'];
- $pos = strpos($host, '.') + 1;
- $suffix = substr($host, $pos);
- }
- return ['http' => $http, 'suffix' => $suffix];
- }
- //获得类似这样 http://m.hhb.com
- public static function getHost()
- {
- return Yii::$app->request->getHostInfo();
- }
- //获取header里的数据 ssh 2019.11.22
- public static function getHeader()
- {
- $ignore = ['host', 'accept', 'content-length', 'content-type'];
- $headers = [];
- foreach ($_SERVER as $key => $value) {
- if (substr($key, 0, 5) === 'HTTP_') {
- $key = substr($key, 5);
- $key = str_replace('_', ' ', $key);
- $key = str_replace(' ', '-', $key);
- $key = strtolower($key);
- if (!in_array($key, $ignore)) {
- $headers[$key] = $value;
- }
- }
- }
- //$token = isset($headers['token']) ? $headers['token'] : '';
- return $headers;
- }
- public static function getToken()
- {
- $token = $_SERVER['HTTP_TOKEN'] ?? '';
- if (empty($token)) {
- $token = Yii::$app->request->get('token', '');
- }
- return $token;
- }
- //获取地址里的account ssh 2019.11.24
- public static function getAccount($url)
- {
- //前端链接,需要去掉链接#号前面部分进行解析
- $pos = strpos($url, '#');
- if (isset($pos)) {
- $url = substr($url, $pos + 1);
- }
- $parse = parse_url($url);
- $account = 0;
- if (isset($parse['query'])) {
- parse_str($parse['query'], $queryArray);
- if (isset($queryArray['account']) && !empty($queryArray['account'])) {
- $account = $queryArray['account'];
- }
- }
- return $account;
- }
- //获取平台id ssh 2019.11.28
- public static function getSourceId()
- {
- $ptSource = Yii::$app->params['ptSource'];
- $host = self::getHost();
- if (empty($host)) {
- return 0;
- }
- $arr = parse_url($host);
- if (isset($arr['host']) == false || empty($arr['host'])) {
- return 0;
- }
- $pos = strpos($arr['host'], '.');
- $ptName = substr($arr['host'], 0, $pos);
- return isset($ptSource[$ptName]) ? $ptSource[$ptName] : 0;
- }
- //判断是否微信浏览器
- public static function isWx()
- {
- if (isset($_SERVER['HTTP_USER_AGENT']) && strpos($_SERVER['HTTP_USER_AGENT'], 'MicroMessenger') == true) {
- return true;
- }
- return false;
- }
- //判断小程序 ssh 2019.12.13
- public static function isMiniProgram()
- {
- //根据头信息判断
- $userAgent = strtolower($_SERVER['HTTP_USER_AGENT']);
- if (strpos($userAgent, 'miniprogram') !== false) {
- return true;
- }
- //根据来源判断
- $referrer = Yii::$app->request->referrer;
- if (strpos($referrer, 'servicewechat.com') !== false) {
- return true;
- }
- return false;
- }
- //获取ip ssh 2019.12.18
- public static function ip()
- {
- return Yii::$app->request->userIP;
- }
- //变成https ssh 2020.2.20
- public static function becomeHttps($url)
- {
- $has = strpos($url, 'https');
- if ($has !== false) {
- return $url;
- }
- return str_replace('http', 'https', $url);
- }
- }
|