百度自动推送代码(PHP版)
$url = 'https://www.xxx.com'; // 你的网站URL地址
$token = '资源秘钥'; //在搜索资源平台申请的推送用的准入密钥,ziyuan.baidu.com普通收录-修改准入密钥查看token
// 百度收录推送API链接地址
$api = "http://data.zz.baidu.com/urls?site=$url&token=$token";
// baidu文章收录推送
echo '百度收录自动推送:<br>';
$file_path = "/news/{$id}.html"; //生成的新闻静态路径
$urls = array($url . $file_path,); //获取新闻推送地址
$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);
$response = json_decode($result, true);
// 初始化一个空的字符串来存储文本描述
$textDescription = "";
// 检查是否返回了错误消息
if (isset($response['error']) && isset($response['message'])) {
$textDescription .= "超过今日推送配额:" . $response['error'] . " - " . $response['message'];
echo $textDescription;
exit; // 如果有错误,输出错误并提前结束脚本
}
// 检查响应中是否存在 "success" 和 "remain" 字段
if (isset($response['success']) && isset($response['remain'])) {
$textDescription .= "成功推送了" . $response['success'] . "条URL。";
$textDescription .= "今日还剩余" . $response['remain'] . "条可推送的URL。\n";
}
// 检查 "not_same_site" 字段
if (isset($response['not_same_site']) && count($response['not_same_site']) > 0) {
$textDescription .= "以下URL由于不是本站URL而未处理:\n";
foreach ($response['not_same_site'] as $url) {
$textDescription .= "- " . $url . "\n";
}
}
// 检查 "not_valid" 字段
if (isset($response['not_valid']) && count($response['not_valid']) > 0) {
$textDescription .= "以下URL不合法:\n";
foreach ($response['not_valid'] as $url) {
$textDescription .= "- " . $url . "\n";
}
}
// 显示文本描述
print_r($textDescription);