透過發送 site:domain 搜尋請求並解析回傳 HTML,提取 Google 顯示的「約 XX 項結果」數字,實現域名收錄量快速查詢。
function get_index($domain) {
$url = "https://www.google.com/search?source=hp&biw=1440&bih=762&q=site%3A$domain&aq=f&aqi=g10&aql=&oq=";
$html = file_get_contents($url);
preg_match('/<div id=resultStats>[Ss].*<nobr>/Ui', $html, $index);
$count = '';
for ($i = 0; $i <= strlen($index[0]); $i++) {
if (is_numeric($index[0][$i])) {
$count .= $index[0][$i];
}
}
return $count;
}
site:example.com 限定特定域名結果id="resultStats" 區塊提取數字/Ui 修飾符支援多行與非貪婪匹配robots.txt 及服務條款,商業用途建議使用官方 API