通過模擬百度搜索請求,分析返回頁面中是否包含目標 URL 的高亮標籤 <b>URL</b> 來判斷收錄狀態。
<?php
/**
* 檢測百度收錄狀態
* @param string $url 待檢測網址
* @return int 1=已收錄 0=未收錄
*/
function checkBaidu($url) {
$searchUrl = 'https://www.baidu.com/s?wd=' . urlencode($url);
$ch = curl_init();
curl_setopt_array($ch, [
CURLOPT_URL => $searchUrl,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_USERAGENT => 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36',
CURLOPT_TIMEOUT => 10,
CURLOPT_SSL_VERIFYPEER => false,
]);
$html = curl_exec($ch);
curl_close($ch);
if (!$html) return 0;
// 提取查詢參數中的實際 URL
$parsed = parse_url($searchUrl);
parse_str($parsed['query'], $qs);
$targetUrl = $qs['wd'] ?? '';
// 移除協議頭與參數
$targetUrl = preg_replace('#^https?://#', '', $targetUrl);
$targetUrl = strtok($targetUrl, '?');
$targetUrl = strtok($targetUrl, '/');
// 關鍵判斷:搜索結果中是否出現 <b>目標域名</b>
return strpos($html, '<b>' . $targetUrl . '</b>') !== false ? 1 : 0;
}
// 測試
var_dump(checkBaidu('https://www.bianceng.cn/'));
?>
原理:百度未收錄時頁面會出現「抱歉,沒有找到與...相關的網頁」字樣。
<?php
function checkBaiduSimple($url) {
$html = @file_get_contents('https://www.baidu.com/s?wd=' . urlencode($url));
if (!$html) return 0;
// 編碼轉換防亂碼
$html = iconv('UTF-8', 'GBK//TRANSLIT', $html);
// 匹配「抱歉,沒有找到與」
return preg_match('/抱歉,沒有找到與/', $html) ? 0 : 1;
}
?>
可結合 site:domain.com 語法批量檢測全站收錄率,或對接百度站長平台 API 獲取官方數據,準確度更高。