必应提取sitemap提交indexnow通用php版

站长CMS吧 11小时前 17

必应收录网站还是很勤快的,中文站建议加入必应的indexnow功能,以下是从sitemap.xml提取出网址推送到indexnow的代码:

<?php

// ========== 配置项 ==========
$host = 'www.domain.com'; // 替换为你的域名
$apiKey = '你的apikey'; // 替换为你的 Bing IndexNow API Key
$logFile = '/www/wwwroot/domain/indexnow_push.log'; // 日志路径(可自定义)

// 多个 sitemap 地址(根据你的实际数量增减)
$sitemapUrls = [
    '你的网址/sitemap/news_6.xml',
    '你的网址/sitemap/news_5.xml',
    '你的网址/sitemap/news_4.xml',
    '你的网址/sitemap/news_3.xml',
    '你的网址/sitemap/news_2.xml',
    '你的网址/sitemap/news_1.xml',
    // 添加更多...
];

echo "正在准备解析多个 sitemap 文件...\n";

$urlList = [];

foreach ($sitemapUrls as $sitemapUrl) {
    echo "正在下载 sitemap: $sitemapUrl\n";
    $xmlContent = @file_get_contents($sitemapUrl);
    if (!$xmlContent) {
        echo "无法获取 sitemap 内容: $sitemapUrl\n";
        continue;
    }

    $xml = simplexml_load_string($xmlContent);
    if (!$xml) {
        echo "无法解析 XML 内容: $sitemapUrl\n";
        continue;
    }

    foreach ($xml->url as $url) {
        $loc = (string)$url->loc;
        if (filter_var($loc, FILTER_VALIDATE_URL)) {
            $urlList[] = $loc;
        }
    }
}

if (empty($urlList)) {
    echo "没有解析到任何有效的 URL。\n";
    file_put_contents($logFile, date("Y-m-d H:i:s") . " - 没有解析到任何有效的 URL。\n", FILE_APPEND);
    exit;
}

echo "共解析到 " . count($urlList) . " 条链接,开始去重...\n";

// ========== 去重 & 分批推送(IndexNow 每次最多 10000)==========
$uniqueUrls = array_values(array_unique($urlList));
$chunks = array_chunk($uniqueUrls, 10000); // 每批最多 10000 条

$totalPushed = 0;

foreach ($chunks as $index => $chunk) {
    echo "正在推送第 " . ($index + 1) . " 批 (" . count($chunk) . " 条)...\n";

    $payload = [
        'host' => $host,
        'key' => $apiKey,
        'urlList' => $chunk
    ];

    $ch = curl_init('https://api.indexnow.org/indexnow');
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_POST, true);
    curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($payload));
    curl_setopt($ch, CURLOPT_HTTPHEADER, [
        'Content-Type: application/json'
    ]);

    $response = curl_exec($ch);
    $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
    curl_close($ch);

    echo "HTTP 状态码: $httpCode\n";
    echo "响应内容: $response\n";

    $totalPushed += count($chunk);

    // 记录日志
    $logMsg = sprintf(
        "批次: %d | 数量: %d | HTTP状态: %d | 响应: %s\n",
        $index + 1,
        count($chunk),
        $httpCode,
        $response
    );
    file_put_contents($logFile, date("Y-m-d H:i:s") . " - " . $logMsg, FILE_APPEND);
}

echo "推送完成,共推送 {$totalPushed} 条链接。\n";

最新回复 (0)
返回