百度搜索
普通收录 API 提交
bash
#!/usr/bin/env bash
###
# 百度搜索 API 提交
# https://ziyuan.baidu.com/linksubmit/index
###
# SITE
SITE_URL=""
SITEMAP_URL="$SITE_URL/sitemap.xml"
# TOKEN
BAIDU_TOKEN=""
# Save url.txt
curl -s "$SITEMAP_URL" | grep -o '<loc>[^<]*</loc>' | sed 's/<loc>\(.*\)<\/loc>/\1/' >urls.txt
# Submit url
curl -H 'Content-Type:text/plain' --data-binary @urls.txt "http://data.zz.baidu.com/urls?site=$SITE_URL&token=$BAIDU_TOKEN"
php
<?php
$site_url = '';
$sitemap_url = $site_url . '/sitemap.xml';
$baidu_token = '';
$xml_data = @file_get_contents($sitemap_url);
if ($xml_data === false) {
die('无法获取 XML 数据');
}
$xmlObject = simplexml_load_string($xml_data);
// 初始化一个数组来保存 URL
$urls = [];
// 遍历每个 <loc> 元素并将其添加到数组中
foreach ($xmlObject->url as $url) {
$urls[] = (string)$url->loc;
}
// 检查 URL 数组是否为空
if (empty($urls)) {
die('未找到任何 URL');
}
$api = "http://data.zz.baidu.com/urls?site=$site_url&token=$baidu_token";
$ch = curl_init();
$options = array(
CURLOPT_URL => $api,
CURLOPT_POST => true,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_POSTFIELDS => implode("\n", $urls),
CURLOPT_HTTPHEADER => array('Content-Type: text/plain'),
);
curl_setopt_array($ch, $options);
$result = curl_exec($ch);
echo $result;