zoukankan      html  css  js  c++  java
  • 获取一个域名的主域名

    class DomainService
    {
        protected $cacheKey = 'domain-cache';
    
        protected $domainList;
    
        public function getDomainSuffix()
        {
            if (!$this->domainList) {
                $cacheList = Yii::$app->cacheFile->get($this->cacheKey);
                if ($cacheList) {
                    $this->domainList = $cacheList;
                } else {
                    $this->domainList = $this->syncRemoteDomain();
                    Yii::$app->cacheFile->set($this->cacheKey, $this->domainList);
                }
            }
    
            return $this->domainList;
        }
    
        public function syncRemoteDomain()
        {
            $res = CurlService::getIns()->get('https://publicsuffix.org/list/effective_tld_names.dat');
            if ($res->getErrorMsg()) {
                return [];
            }
    
            $body = $res->getBody();
            $domainSuffixList = explode("
    ", $body);
            $domainSuffixList = array_filter($domainSuffixList, function ($row) {
                $row = trim($row);
                if (!empty($row) && substr($row, 0, 2) != '//') {
                    return true;
                }
                return false;
            });
    
            return $domainSuffixList;
        }
    
        public function getMain(string $domain)
        {
            $domainList = $this->getDomainSuffix();
            $domainMap = array_flip($domainList);
            $origin = $domainSep = explode('.', $domain);
            $len = count($domainSep);
            for ($i = 0; $i < $len; $i++) {
                unset($domainSep[$i]);
                if (count($domainSep) == 0) {
                    break;
                }
                $s = implode('.', $domainSep);
                if (isset($domainMap[$s])) {
                    return '*.'.$origin[$i].'.'.$s;
                }
            }
    
            return $domain;
        }
    }
  • 相关阅读:
    iOS API 概述
    iOS开发的一些奇巧淫技3
    iOS开发的一些奇巧淫技1&2
    iOS-一键拨号
    iOS层次架构
    简单block 和 代理
    iOS开发-单例GCD
    简单的归档、接档
    通知中心与本地通知
    安装linux centos 7.7 遇到 DRM:Pointer to TMDS table invalid
  • 原文地址:https://www.cnblogs.com/a-flydog/p/12145745.html
Copyright © 2011-2022 走看看