檢查url是否被百度收錄源碼
使用php 開(kāi)發(fā)檢查url是否被百度收錄源碼:一下代碼只返回 true false 1.true收錄 false未收錄,以下檢查不是百分比正確,要以收錄為準(zhǔn)
<?php function isUrlIndexedByBaidu($url) { // 百度搜索的API或查詢字符串參數(shù) $baiduSearchUrl = 'https://www.baidu.com/s?wd=' . urlencode("info:{$url}"); // 初始化cURL會(huì)話 $ch = curl_init(); // 設(shè)置cURL選項(xiàng) curl_setopt($ch, CURLOPT_URL, $baiduSearchUrl); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); // 獲取的信息以字符串返回,而不是直接輸出。 curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.88 Safari/537.36"); curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); // 允許重定向 curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); // 不驗(yàn)證SSL證書(shū) curl_setopt($ch, CURLOPT_TIMEOUT, 10); // 設(shè)置超時(shí)時(shí)間 // 執(zhí)行cURL會(huì)話并獲取結(jié)果 $htmlContent = curl_exec($ch); // 檢查是否有錯(cuò)誤發(fā)生 if (curl_errno($ch)) { echo 'Curl error: ' . curl_error($ch); return false; } // 關(guān)閉cURL資源,并釋放系統(tǒng)資源 curl_close($ch); // 檢查是否包含“沒(méi)有找到相關(guān)網(wǎng)頁(yè)”的字樣,這表示該網(wǎng)址未被百度收錄 if (strpos($htmlContent, '沒(méi)有找到相關(guān)網(wǎng)頁(yè)') !== false) { return false; } // 如果找到了,則認(rèn)為該網(wǎng)址已經(jīng)被百度收錄 return true; }
使用示例
$urlToCheck = 'http://example.com'; if (isUrlIndexedByBaidu($urlToCheck)) { echo "URL 已被百度收錄"; } else { echo "URL 未被百度收錄"; } ?>
關(guān)鍵詞: 百度收錄