分享下bing的indexnow通用php版sitemap提交
鐮旂┒浜嗕竴涓櫄涓婏紝澶у鎷垮幓鐢ㄥ惂銆傚笣鍥絚ms 鏄撲紭 绛塸hp绋嬪簭閮藉彲浠ョ敤锛
- <?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 "馃寪 姝e湪鍑嗗瑙f瀽澶氫釜 sitemap 鏂囦欢...\n";$urlList = [];foreach ($sitemapUrls as $sitemapUrl) { echo "馃摜 姝e湪涓嬭浇 sitemap: $sitemapUrl\n"; $xmlContent = @file_get_contents($sitemapUrl); if (!$xmlContent) { echo "鈿狅笍 鏃犳硶鑾峰彇 sitemap 鍐呭: $sitemapUrl\n"; continue; } $xml = simplexml_load_string($xmlContent); if (!$xml) { echo "鈿狅笍 鏃犳硶瑙f瀽 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 "鈿狅笍 娌℃湁瑙f瀽鍒颁换浣曟湁鏁堢殑 URL銆俓n"; file_put_contents($logFile, date("Y-m-d H:i:s") . " - 鈿狅笍 娌℃湁瑙f瀽鍒颁换浣曟湁鏁堢殑 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 "馃摛 姝e湪鎺ㄩ佺 " . ($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";