首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 編程語言 >  >> 網頁程式設計 >> PHP 程式設計 >> PHP 相關知識

PHP 透過 API 獲取愛站網與站長工具百度權重值

     編輯:PHP 相關知識

百度權重是站長評估網站流量潛力的常用指標。愛站網與站長工具各有獨立算法,本文提供兩個 PHP 函式分別串接其非官方 API 獲取權重值,並說明使用限制與替代方案。

1. 獲取愛站網百度權重

<?php
/**
 * 取得愛站網百度權重
 * @param string $domain 完整域名,如 www.example.com
 * @return int 權重值 0-10,失敗回傳 0
 */
function getAizhanBaiduWeight($domain) {
    $url = "https://www.aizhan.com/getbr.php?url=" . urlencode($domain) . "&style=1";
    
    $ctx = stream_context_create([
        'http' => [
            'timeout' => 5,
            'header' => "User-Agent: Mozilla/5.0 (compatible; PHP Bot)\r\n"
        ]
    ]);
    
    $content = @file_get_contents($url, false, $ctx);
    if ($content === false) return 0;
    
    // 回傳格式通常為純數字或簡單 HTML 片段
    if (preg_match('/\b([0-9]|10)\b/', $content, $m)) {
        return (int)$m[1];
    }
    return 0;
}

// 使用範例
echo getAizhanBaiduWeight('www.baidu.com'); // 輸出如 9
?>

2. 獲取站長工具百度權重

<?php
/**
 * 取得站長工具百度權重
 * @param string $domain 域名
 * @return int 權重值 0-10,失敗回傳 0
 */
function getChinazBaiduWeight($domain) {
    $url = "https://mytool.chinaz.com/baidusort.aspx?host=" . urlencode($domain);
    
    $ctx = stream_context_create([
        'http' => [
            'timeout' => 8,
            'header' => "User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36\r\n"
        ]
    ]);
    
    $html = @file_get_contents($url, false, $ctx);
    if ($html === false) return 0;
    
    // 頁面結構可能變動,需定期維護選擇器
    // 典型特徵:9 或類似
    if (preg_match('/class=["\']br["\'][^>]*>\s*([0-9]|10)\s*

3. 完整封裝類別 (支援快取、異常處理)

<?php
class BaiduWeightFetcher {
    private $cacheDir;
    private $ttl = 3600; // 1 小時快取

    public function __construct($cacheDir = __DIR__ . '/cache') {
        $this->cacheDir = $cacheDir;
        if (!is_dir($cacheDir)) mkdir($cacheDir, 0755, true);
    }

    public function getWeight($domain, $source = 'aizhan') {
        $cacheFile = $this->cacheDir . '/' . md5($domain . '_' . $source) . '.json';
        
        // 讀取快取
        if (file_exists($cacheFile) && (time() - filemtime($cacheFile) < $this->ttl)) {
            $data = json_decode(file_get_contents($cacheFile), true);
            if ($data) return $data['weight'];
        }

        $weight = 0;
        try {
            if ($source === 'aizhan') $weight = $this->fetchAizhan($domain);
            elseif ($source === 'chinaz') $weight = $this->fetchChinaz($domain);
        } catch (Exception $e) {
            error_log("BaiduWeightFetcher error: " . $e->getMessage());
        }

        // 寫入快取
        file_put_contents($cacheFile, json_encode(['weight' => $weight, 'time' => time()]));
        return $weight;
    }

    private function fetchAizhan($domain) {
        $url = "https://www.aizhan.com/getbr.php?url=" . urlencode($domain) . "&style=1";
        $content = $this->httpGet($url);
        return preg_match('/\b([0-9]|10)\b/', $content) ? (int)$m[1] : 0;
    }

    private function fetchChinaz($domain) {
        $url = "https://mytool.chinaz.com/baidusort.aspx?host=" . urlencode($domain);
        $html = $this->httpGet($url);
        if (preg_match('/class=["\']br["\'][^>]*>\s*([0-9]|10)\s* ['timeout' => 10, 'header' => "User-Agent: Mozilla/5.0\r\n"]
        ]);
        return @file_get_contents($url, false, $ctx) ?: '';
    }
}

// 使用
$fetcher = new BaiduWeightFetcher();
echo "愛站權重: " . $fetcher->getWeight('www.example.com', 'aizhan') . "\n";
echo "站長權重: " . $fetcher->getWeight('www.example.com', 'chinaz') . "\n";
?>

⚠️ 重要限制與風險

  1. 非官方 API:介面隨時可能變更、限流或下線,生產環境請做好降級與監控。
  2. IP 限流:高頻請求會被封禁,建議加入快取 (如上例) 與請求間隔。
  3. 權重定義不同:愛站與站長工具算法不同 (站長工具剔除低指數詞),數值不可直接比較。
  4. 法律合規:爬蟲行為需遵守目標站 robots.txt 及服務條款。

替代方案

  • 使用官方 百度站長平台 API (需驗證站點擁有權) 獲取精準流量數據。
  • 第三方付費 SEO API (如 5118、Aizhan 開放平台) 提供更穩定、豐富的指標。
  • 自建關鍵詞排名監控系統,依據實際排名試算流量權重。
  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved