$url . '/sitemap.xml', 'api' => 'http://data.zz.baidu.com/urls?site=' . $url . '&token=' . $token ]; } } foreach ($data as $value) { echo '================================ '; echo $value['url'] . ' '; post_Baidu($value['url'], $value['api']); } } else { echo "文件 $file 不存在或无法打开!"; } function post_Baidu($xml_url, $baidu_api) { $xmldata = @file_get_contents($xml_url); // 使用@符号忽略文件获取时可能产生的警告 if ($xmldata === false) { echo "无法获取 sitemap.xml 文件内容或链接无效: "; return; } $xmlstring = simplexml_load_string($xmldata, 'SimpleXMLElement', LIBXML_NOCDATA); if ($xmlstring === false) { echo "无法解析 sitemap.xml 文件内容: "; return; } $value_array = json_decode(json_encode($xmlstring), true); if (!isset($value_array['url']) || !is_array($value_array['url'])) { echo "解析后的 sitemap.xml 内容不符合预期: "; return; } $url = []; $count_limit = 10; // 最多推送前10条链接 for ($i = 0; $i < min($count_limit, count($value_array['url'])); $i++) { $url[] = $value_array['url'][$i]['loc']; } $ch = curl_init(); $options = array( CURLOPT_URL => $baidu_api, CURLOPT_POST => true, CURLOPT_RETURNTRANSFER => true, CURLOPT_POSTFIELDS => implode("\n", $url), CURLOPT_HTTPHEADER => array('Content-Type:text/plain'), ); curl_setopt_array($ch, $options); $result = curl_exec($ch); if ($result === false) { echo "推送过程中出现错误: "; } else { echo '推送结果:' . $result . ' '; // 输出推送结果 } curl_close($ch); } ?>
apis.txt
格式如下
https://www.xxx.cc&token=fNyzgGec2m5ovcM0
https://xxx.cc&token=LCkCXGicwsdPlLYW