針對 SEO 需求,需批量檢查域名在搜狗的收錄情況或權重值。本文提供一個基於 file_get_contents 的極簡腳本,從文本文件讀取域名列表,逐行請求搜狗排名接口,解析返回的 sogourank=數值,篩選權重 > 2 的域名輸出。
<?php
date_default_timezone_set('Asia/Shanghai');
header('Content-Type: text/html; charset=utf-8');
$listFile = 'list.txt'; // 每行一個域名,如 example.com
if (!is_file($listFile)) exit("文件 $listFile 不存在");
$domains = array_filter(array_map('trim', file($listFile, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES)));
if (!$domains) exit('無有效域名');
foreach ($domains as $domain) {
$api = "https://rank.ie.sogou.com/sogourank.php?ur=" . urlencode("https://$domain/");
$html = @file_get_contents($api);
if ($html === false) {
echo "❌ $domain 請求失敗
";
continue;
}
// 返回格式:sogourank=3
$rank = (int)str_replace('sogourank=', '', trim($html));
if ($rank > 2) {
echo "✅ $domain : Sogou Rank = $rank
";
} else {
echo "⚪ $domain : Rank $rank (≤2)
";
}
usleep(200000); // 0.2s 延遲,禮貌訪問避免被封
}
rank.ie.sogou.com/sogourank.php 為非官方公開接口,隨時可能失效或限流,生產環境請申請官方 API 或自建爬蟲池。urlencode() 處理域名中可能包含的特殊字符。@file_get_contents 抑制警告,配合 === false 判斷網絡失敗。usleep(200000) 限制頻率約 5 req/s,減少被封風險。curl_multi / Swoole 協程 / Guzzle Promise 並發請求,百萬級域名數小時完成。批量查詢屬於自動化訪問,請遵守目標站點 robots.txt、服務條款,控制頻率,避免構成「非法侵入計算機信息系統罪」或民事侵權。建議優先使用官方開放平台或授權數據服務商。