Typecho刷文章访问次数
因为手动修改数据库显得不真实,随手撸了一段PHP代码,该代码会自动读取Sitemap.xml文件,并在每次访问后随机访问sitemap其中的一个地址。这是真实访问量。PHP可以设置定时任务,例如一分钟执行一次。
其它的不多说,直接上代码,注意修改为自己的站点地图地址:
<?php
function getRandomSitemapLink($sitemapUrl) {
// Download the sitemap file using cURL
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $sitemapUrl);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$sitemapData = curl_exec($ch);
curl_close($ch);
if (!$sitemapData) {
die("Failed to download the sitemap.");
}
// Parse the sitemap XML
$xml = simplexml_load_string($sitemapData);
$links = [];
foreach ($xml->url as $url) {
$links[] = (string)$url->loc;
}
// Choose a random link from the sitemap
if (count($links) === 0) {
die("No links found in the sitemap.");
}
$randomLink = $links[array_rand($links)];
return $randomLink;
}
function visitRandomSitemapLink($sitemapUrl) {
$randomLink = getRandomSitemapLink($sitemapUrl);
echo "Visiting: $randomLink\n";
// Use cURL to visit the random link
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $randomLink);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_exec($ch);
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
echo "HTTP Status Code: $httpCode\n";
}
// Replace 'your_sitemap_url_here' with the actual URL of the sitemap
$sitemapUrl = 'https://www.663333.xyz/sitemap.xml';
// Call the function to visit a random link from the sitemap
visitRandomSitemapLink($sitemapUrl);
?>本作品采用 知识共享署名-相同方式共享 4.0 国际许可协议 进行许可。
小菜鸡
评论已关闭