xml地图提取出链接自动提交到必应IndexNow


一个php页面,可以从网站xml地图中提取链接自动提交到必应IndexNow。

以下是代码:

<?php
// 配置参数
$sitemapUrl = 'https://yourdomain.com/sitemap.xml';  // XML地图路径
$apiKey = 'your_indexnow_key'; // IndexNow密钥(需提前在网站根目录放置key文件)
$endpoint = 'https://www.bing.com/IndexNow';  // 必应API接口
$batchSize = 50; // 每次提交数量(必应限制单次最多100条)

// 解析XML地图
$xml = simplexml_load_file($sitemapUrl);
$urls = [];
foreach ($xml->url as $url) {
    $urls[] = (string)$url->loc;
}

// 分批提交
$total = count($urls);
$chunks = array_chunk($urls, $batchSize);
foreach ($chunks as $index => $chunk) {
    $postData = [
        'host' => parse_url($sitemapUrl, PHP_URL_HOST),
        'key' => $apiKey,
        'keyLocation' => "https://".parse_url($sitemapUrl, PHP_URL_HOST)."/{$apiKey}.txt",
        'urlList' => $chunk
    ];

    $response = sendIndexNowRequest($endpoint, $postData);
    echo "批次 ".($index+1)."/".count($chunks)." 提交状态: ".$response['http_code']."<br>";
}

// HTTP请求函数
function sendIndexNowRequest($url, $data) {
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_POST, true);
    curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));
    curl_setopt($ch, CURLOPT_HTTPHEADER, [
        'Content-Type: application/json; charset=utf-8'
    ]);
    $response = curl_exec($ch);
    $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
    curl_close($ch);
    return ['http_code' => $httpCode, 'response' => $response];
}
?>

上一篇:网站添加复制文字自动添加版权功能
下一篇:没了
最新回复 (0)
返回