php代码|自己2025年8月28日之前使用的获取链接代码

时间:2025-08-28 08:41:22   阅读:21
老版本的

/*  发卡网地址  */
/*随机域名*/
/*调用方法 echo $newDomain;*/
// 远程域名文件URL
$url = 'https://hutoucun.cn/txt/yuming.txt';
// 缓存文件路径
$cacheFile = __DIR__ . '/domain_cache.txt';
// 缓存有效期(秒)
$cacheExpire = 3600; // 1小时
$domains = [];
$error = '';
$useExpiredCache = false;

// 检查缓存文件是否存在且未过期
if (file_exists($cacheFile) && time() - filemtime($cacheFile) < $cacheExpire) {
    $cacheContent = file_get_contents($cacheFile);
    if ($cacheContent !== false) {
        $domains = json_decode($cacheContent, true);
        if (!is_array($domains)) {
            $domains = [];
        }
    }
}

// 如果缓存不存在或已过期,尝试从远程获取
if (empty($domains)) {
    $oldCache = [];
    // 尝试读取旧缓存(即使已过期)
    if (file_exists($cacheFile)) {
        $oldCacheContent = file_get_contents($cacheFile);
        if ($oldCacheContent !== false) {
            $oldCache = json_decode($oldCacheContent, true);
            if (!is_array($oldCache)) {
                $oldCache = [];
            }
        }
    }

    // 尝试使用cURL获取远程文件(优先方法)
    if (function_exists('curl_init')) {
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10);
        curl_setopt($ch, CURLOPT_TIMEOUT, 30);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); // 生产环境建议验证SSL
        $content = curl_exec($ch);
        $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
        curl_close($ch);
        
        if ($httpCode == 200 && $content) {
            $domains = explode("\n", trim($content));
            // 更新缓存文件
            file_put_contents($cacheFile, json_encode($domains));
        } else {
            $error = "cURL请求失败,HTTP状态码: $httpCode";
            if (!empty($oldCache)) {
                $domains = $oldCache;
                $useExpiredCache = true;
            }
        }
    } 
    // 备选方案:使用file_get_contents
    elseif (ini_get('allow_url_fopen')) {
        $context = stream_context_create([
            'http' => [
                'method' => 'GET',
                'timeout' => 30,
                'header' => "User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64)"
            ]
        ]);
        
        if ($content = @file_get_contents($url, false, $context)) {
            $domains = explode("\n", trim($content));
            // 更新缓存文件
            file_put_contents($cacheFile, json_encode($domains));
        } else {
            $error = "file_get_contents请求失败";
            if (!empty($oldCache)) {
                $domains = $oldCache;
                $useExpiredCache = true;
            }
        }
    } 
    // 无可用方法时的错误处理
    else {
        $error = "无法获取远程文件:未启用cURL且allow_url_fopen被禁用";
        if (!empty($oldCache)) {
            $domains = $oldCache;
            $useExpiredCache = true;
        }
    }
    
    // 记录错误日志(如果有错误且使用了过期缓存)
    if ($useExpiredCache && !empty($error)) {
        error_log("[" . date('Y-m-d H:i:s') . "] 域名获取失败,使用过期缓存: $error\n", 3, __DIR__ . '/domain_fetch_errors.log');
    }
}

// 处理域名列表
if (!empty($domains)) {
    // 分离二级域名和一级域名
    $secondLevelDomains = [];
    $firstLevelDomains = [];
    
    foreach ($domains as $domain) {
        // 清理域名格式
        $domain = trim($domain);
        if (empty($domain)) continue;
        
        // 去除协议和路径部分
        $domain = preg_replace('#^https?://#', '', $domain);
        $domain = explode('/', $domain)[0];
        
        if (substr_count($domain, '.') >= 2) {
            $secondLevelDomains[] = $domain;
        } else {
            $firstLevelDomains[] = $domain;
        }
    }
    
    // 优先选择二级域名
    if (!empty($secondLevelDomains)) {
        $randomDomain = $secondLevelDomains[array_rand($secondLevelDomains)];
        $newDomain = $randomDomain;
    } else {
        // 如果没有二级域名,选择一级域名
        $randomDomain = $firstLevelDomains[array_rand($firstLevelDomains)];
        
        // 生成3-10个随机字母作为前缀
        $length = rand(3, 10);
        $characters = 'abcdefghijklmnopqrstuvwxyz';
        $prefix = '';
        
        for ($i = 0; $i < $length; $i++) {
            $prefix .= $characters[rand(0, strlen($characters) - 1)];
        }
        
        // 组合成新的二级域名
        $newDomain = $prefix . '.' . $randomDomain;
    }
    
    // 添加协议前缀
    if (strpos($newDomain, 'https://') === false) {
        $newDomain = 'https://' . $newDomain;
    }
    
    // 如果使用了过期缓存,在域名后添加提示(生产环境可移除)
    if ($useExpiredCache) {
        $newDomain .= '?keywords=使用过期缓存';
    }
} 
// 处理错误情况
else {
    // 生产环境建议记录日志而非直接显示错误
    $newDomain = isset($error) ? "错误:$error" : "错误:无法获取或解析域名列表";
}

// 输出完整域名


上一篇:8月11日星期一,农历闰六月十八,工作愉快,平安喜乐

网友评论