emlog Pro文章主动推送百度SEO 搜索引擎优化(SEO)
ls4.cn • 12 个月前 • 215 次点击 • 网络教程
当用户访问文章自动推送送到百度,让优质内容更快收录,同时也节省配额。
模板中创建一个php文件比如我(baidutuisong.php)代码中添加网址和密钥
<?php
// 动态获取当前页面的完整 URL
$protocol = isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] === 'on' ? 'https' : 'http';
$domain = $_SERVER['HTTP_HOST'];
$currentPageUrl = $protocol . '://' . $domain . $_SERVER['REQUEST_URI'];
// 检查是否已经推送过
$submittedUrlsFile = '/submitted_urls.txt';
$submittedUrls = file_exists($submittedUrlsFile) ? file($submittedUrlsFile, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES) : [];
// 检查链接是否已提交
if (in_array($currentPageUrl, $submittedUrls)) {
// 链接已推送过,不再重复提交
// 这里不需要输出任何内容到浏览器
exit;
}
// 推送链接到百度ZZ
$apiUrl = 'http://data.zz.baidu.com/urls?site=网址&token=密钥';
$urlsToPush = [$currentPageUrl]; // 推送单个链接
$ch = curl_init();
$options = [
CURLOPT_URL => $apiUrl,
CURLOPT_POST => true,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_POSTFIELDS => implode("\n", $urlsToPush),
CURLOPT_HTTPHEADER => ['Content-Type: text/plain'],
];
curl_setopt_array($ch, $options);
$result = curl_exec($ch);
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
// 检查推送结果
if ($httpCode == 200) {
$response = json_decode($result, true);
if ($response && isset($response['success']) && $response['success'] > 0) {
// 推送成功,保存到文本文件
file_put_contents($submittedUrlsFile, $currentPageUrl . PHP_EOL, FILE_APPEND | LOCK_EX);
// 提取并显示剩余推送次数
if (isset($response['remain'])) {
$remain = $response['remain'];
echo "推送成功,当天剩余的可推送URL条数:" . $remain;
} else {
echo "推送成功,但返回结果中未包含剩余推送次数信息。";
}
} else {
// 推送失败但状态码为200,可能是链接无效等原因
// 将错误信息记录到日志文件中
error_log("推送失败但状态码为200: " . json_encode($response) . ", URL: " . $currentPageUrl);
}
} else {
// 推送失败,非200状态码
// 将错误信息记录到日志文件中
error_log("推送失败,状态码: " . $httpCode . ", URL: " . $currentPageUrl . ", 响应内容: " . $result);
}
?>
然后在文章页面放上钩子(<?php include 'baidutuisong.php'; ?>)就可以了。
扫描二维码,在手机上阅读
推荐阅读: