You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

向NextDNS Denylist批量提交域名时出现notFound错误求助

向NextDNS配置的黑名单批量添加域名时返回notFound错误排查

我尝试把blacklist_domains.txt里的多个域名提交到NextDNS配置的denylist,但一直报错。对照API文档还是没找到问题,怀疑是端点URL不对。下面是我的PHP代码和错误输出,麻烦帮忙排查:

原始PHP代码

<?php
// Hardcoded Configurations
$nextdns_api_key = "API-KEY";
$nextdns_profile = "configID"; // Replace with your actual Config ID

// API Endpoint URL
$api_endpoint = "https://api.nextdns.io/profiles/$nextdns_profile/denylist";

// API Auth
$header = [
    "Content-Type: application/json",
    "X-Api-Key: $nextdns_api_key"
];

// Initialize count_domains
$count_domains = 0;

// Open Domains File
$blacklistfile = fopen('blacklist_domains.txt', 'r');
if ($blacklistfile) {
    // Read the file line by line
    while (($domain = fgets($blacklistfile)) !== false) {
        $count_domains++;

        $response = denylist_blockdomain(trim($domain), $header, $api_endpoint);
        echo "Domain: $domain";
        echo "Response: $response" . PHP_EOL;

        // Check for errors in the response
        $json_response = json_decode($response, true);

        if (!empty($json_response) && isset($json_response['errors'])) {
            foreach ($json_response['errors'] as $error) {
                echo "Error Code: " . $error['code'] . PHP_EOL;
                echo "Error Detail: " . ($error['detail'] ?? '') . PHP_EOL;
                echo "Error Source: " . ($error['source']['parameter'] ?? '') . PHP_EOL;
            }
        }

        flush();
        sleep(2); // Adjust sleep time based on rate-limiting policies
    }

    // Close file
    fclose($blacklistfile);
} else {
    echo "Error opening the file." . PHP_EOL;
}

echo $count_domains . " Added to blacklist" . PHP_EOL;
echo "Done, exiting app" . PHP_EOL;

// Function definitions (replace with actual implementations)
function denylist_blockdomain($domain, $header, $api_endpoint) {
    // Implementation for blocking a domain in denylist
    // Example using curl:
    $data = json_encode(["denylist" => [["id" => $domain, "active" => true]]]);
    echo "Request Data: $data" . PHP_EOL;
    $ch = curl_init($api_endpoint);
    curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
    curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_HTTPHEADER, $header);

    $response = curl_exec($ch);
    curl_close($ch);

    return $response;
}
?>

错误输出

Domain: example1.net
Response: {"errors":[{"code":"notFound"}]}
Error Code: notFound
Error Detail: 
Error Source: 
Domain: example2.net
Response: {"errors":[{"code":"notFound"}]}
Error Code: notFound
Error Detail: 
Error Source: 
Domain: example3.com
Response: {"errors":[{"code":"notFound"}]}
Error Code: notFound
Error Detail: 
Error Source: 
3 Added to blacklist
Done, exiting app

问题分析与修复方案

核心问题

notFound错误的主要原因是API端点URL和请求格式不匹配NextDNS的API规范:

  1. 单个域名添加到denylist的正确端点是POST https://api.nextdns.io/profiles/{profileId}/denylist/{domain},而非你当前使用的批量端点
  2. 单个域名添加不需要构造嵌套的denylist数组,请求体只需空JSON对象即可

修正后的PHP代码

<?php
// 硬编码配置
$nextdns_api_key = "API-KEY";
$nextdns_profile = "configID"; // 替换为实际配置ID

// API端点基础URL
$api_endpoint_base = "https://api.nextdns.io/profiles/$nextdns_profile/denylist";

// API认证头
$header = [
    "Content-Type: application/json",
    "X-Api-Key: $nextdns_api_key"
];

// 初始化域名计数
$count_domains = 0;

// 打开域名文件
$blacklistfile = fopen('blacklist_domains.txt', 'r');
if ($blacklistfile) {
    // 逐行读取文件
    while (($domain = fgets($blacklistfile)) !== false) {
        $domain = trim($domain);
        if (empty($domain)) continue; // 跳过空行
        $count_domains++;

        $response = denylist_blockdomain($domain, $header, $api_endpoint_base);
        echo "域名: $domain\n";
        echo "响应: $response\n";

        // 检查响应中的错误
        $json_response = json_decode($response, true);

        if (!empty($json_response) && isset($json_response['errors'])) {
            foreach ($json_response['errors'] as $error) {
                echo "错误代码: " . ($error['code'] ?? '') . "\n";
                echo "错误详情: " . ($error['detail'] ?? '') . "\n";
                echo "错误来源: " . ($error['source']['parameter'] ?? '') . "\n";
            }
        } else {
            echo "域名添加成功\n";
        }

        flush();
        sleep(2); // 根据限流策略调整等待时间
    }

    // 关闭文件
    fclose($blacklistfile);
} else {
    echo "打开文件失败。\n";
}

echo $count_domains . " 个域名尝试添加到黑名单\n";
echo "操作完成,退出程序\n";

// 单个域名添加到黑名单的函数
function denylist_blockdomain($domain, $header, $api_endpoint_base) {
    // 构造单个域名的API端点(URL编码避免特殊字符问题)
    $api_endpoint = $api_endpoint_base . "/" . urlencode($domain);
    
    $ch = curl_init($api_endpoint);
    curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
    curl_setopt($ch, CURLOPT_POSTFIELDS, "{}"); // 空JSON体符合API要求
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_HTTPHEADER, $header);

    // 排查CURL自身错误
    if(curl_errno($ch)){
        echo "CURL错误: " . curl_error($ch) . "\n";
    }
    $response = curl_exec($ch);
    curl_close($ch);

    return $response;
}
?>

额外排查建议

  1. 验证配置ID和API密钥有效性:
    用curl手动测试端点,替换成你的配置ID和API密钥:
    curl -X POST "https://api.nextdns.io/profiles/你的配置ID/denylist/example.com" -H "X-Api-Key: 你的API密钥" -H "Content-Type: application/json" -d "{}"
    
  2. 确认配置ID正确性:配置ID是NextDNS控制台配置页面URL中的一串字符(例如https://my.nextdns.io/abc123中的abc123)
  3. 检查API密钥权限:确保生成API密钥时,已勾选对应配置的编辑权限

内容的提问来源于stack exchange,提问作者Zack Tim

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.06 12:48:21