这是一个简单的 PHP 脚本,用于检查网站在 Google 上针对给定关键字的排名:
<?php
function checkKeywordPosition($keyword, $websiteUrl) {
$googleUrl = "http://www.google.com/search?q=" . urlencode($keyword) . "&num=100";
$page = file_get_contents($googleUrl);
// Check if the page was fetched successfully
if ($page === FALSE) {
return "Error: Unable to fetch Google search results.";
}
// Find the position of the website in search results
$position = strpos($page, $websiteUrl);
// If the website is found, return its position
if ($position !== FALSE) {
// Google starts indexing from 1 instead of 0
return "The website is found at position " . ($position + 1);
} else {
return "The website is not found in the top 100 results.";
}
}
// Example usage
$keyword = "your keyword here";
$websiteUrl = "your website URL here";
$result = checkKeywordPosition($keyword, $websiteUrl);
echo $result;
?>
此脚本将关键字和网站网址作为输入参数,获取该关键字的 Google 搜索结果,然后检查给定网站网址在这些搜索结果中的位置。如果找到该网站,它将返回位置,否则将返回一条消息,表明该网站未在前 100 个结果中找到。请记住,此脚本使用简单的文件检索方法,由于安全限制或网络配置,这些方法可能无法在所有环境中工作。此外,抓取搜索引擎结果可能违反其服务条款,因此请负责任地使用它并考虑使用搜索引擎为此目的提供的 API。
评论 (0)
这里还没有评论,你可以成为第一个评论者!