编程笔记

编程笔记

一个php获取B站api,自动生成首页生成静态视频页面
2025-03-07

只需要一个php文件自动获取视频,自动获取无限视频!并生成静态播放页面!

首先在根目录创建一个php文件,命名index.php

写入代码:

<?php
// 定义每页显示的视频数量
$videosPerPage = 50;

// 获取当前页码,默认为第 1 页
$page = isset($_GET['page']) ? intval($_GET['page']) : 1;
$offset = ($page - 1) * $videosPerPage;

// 定义 B 站热门视频 API 地址
$apiUrl = "https://api.bilibili.com/x/web-interface/popular?ps=$videosPerPage&pn=$page";

// 初始化 cURL 会话
$ch = curl_init();

// 设置 cURL 选项
curl_setopt($ch, CURLOPT_URL, $apiUrl);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
// 设置请求头,模拟浏览器请求
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
    'User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.3'
));

// 执行 cURL 请求
$response = curl_exec($ch);

// 检查是否有错误
if (curl_errno($ch)) {
    echo 'Curl error: '. curl_error($ch);
}

// 关闭 cURL 会话
curl_close($ch);

// 解析 JSON 响应
$data = json_decode($response, true);

// 检查响应是否成功
if ($data && $data['code'] === 0) {
    $videos = $data['data']['list'];

    // 创建保存 HTML 文件的目录
    $directory = 'video_pages';
    if (!is_dir($directory)) {
        mkdir($directory, 0777, true);
    }

    foreach ($videos as $video) {
        $title = $video['title'];
        $bvid = $video['bvid'];
        $encodedTitle = urlencode($title);

        // 生成相关视频导航列表(横排)
        $relatedVideosHtml = '<h2>相关视频</h2><div>';
        $count = 0;
        foreach ($videos as $relatedVideo) {
            if ($relatedVideo['bvid'] !== $bvid && $count < 20) {
                $relatedTitle = $relatedVideo['title'];
                $relatedBvid = $relatedVideo['bvid'];
                $relatedVideosHtml .= '<a href="video_pages/'.$relatedBvid.'.html">'.$relatedTitle.'</a>';
                $count++;
            }
        }
        $relatedVideosHtml .= '</div>';

        // 生成往期视频随机导航列表(横排)
        $allBvids = [];
        foreach ($videos as $v) {
            if ($v['bvid'] !== $bvid) {
                $allBvids[] = $v;
            }
        }
        shuffle($allBvids);
        $pastVideosHtml = '<h2>往期热门视频</h2><div>';
        $count = 0;
        foreach ($allBvids as $pastVideo) {
            if ($count < 20) {
                $pastTitle = $pastVideo['title'];
                $pastBvid = $pastVideo['bvid'];
                $pastVideosHtml .= '<a href="video_pages/'.$pastBvid.'.html">'.$pastTitle.'</a>';
                $count++;
            }
        }
        $pastVideosHtml .= '</div>';

        // 生成 HTML 文件内容
        $htmlContent = <<<HTML
<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta name="description" content="观看 B 站热门视频 - $title,精彩内容不容错过。">
    <meta name="keywords" content="B站视频, $title">
    <title>$title</title>
    <style>
        body {
            font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
            margin: 0;
            padding: 0;
            background-color: #f4f4f4;
        }
        header {
            background-color: #007BFF;
            color: white;
            text-align: center;
            padding: 20px 0;
        }
        main {
            padding: 20px;
            text-align: center;
        }
        .related-videos-list, .past-videos-list {
            display: flex;
            flex-wrap: wrap;
            gap: 10px;
            margin-top: 10px;
            justify-content: center;
        }
        .related-videos-list a, .past-videos-list a {
            background-color: white;
            padding: 8px 12px;
            border-radius: 5px;
            box-shadow: 0 0 5px rgba(0, 0, 0, 0.1);
            text-decoration: none;
            color: #007BFF;
        }
        .related-videos-list a:hover, .past-videos-list a:hover {
            text-decoration: underline;
        }
        footer {
            background-color: #333;
            color: white;
            text-align: center;
            padding: 10px 0;
            position: fixed;
            bottom: 0;
            width: 100%;
        }
    </style>
</head>
<body>
    <header>
        <h1>$title</h1>
    </header>
    <main>
        <iframe src="https://player.bilibili.com/player.html?bvid=$bvid&page=1" scrolling="no" border="0" frameborder="no" framespacing="0" allowfullscreen="true" width="80%" height="600px"></iframe>
    </main>
    <div>
        $relatedVideosHtml
        $pastVideosHtml
    </div>
    <footer>
        <p>版权所有 &copy; 2025 你的网站名称</p>
    </footer>
</body>
</html>
HTML;

        // 生成 HTML 文件路径
        $htmlFileName = $directory . '/' . $bvid . '.html';

        // 写入文件
        file_put_contents($htmlFileName, $htmlContent);
    }
    ?>
    <!DOCTYPE html>
    <html>
    <head>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <meta name="description" content="浏览 B 站热门视频,涵盖各种精彩内容。支持分页查看。">
        <meta name="keywords" content="B站热门视频, 视频导航, 分页查看">
        <title>B 站热门视频导航 - 第 <?php echo $page; ?> 页</title>
        <style>
            body {
                font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
                margin: 0;
                padding: 0;
                background-color: #f4f4f4;
            }
            header {
                background-color: #007BFF;
                color: white;
                text-align: center;
                padding: 20px 0;
            }
            nav {
                background-color: #333;
                color: white;
            }
            nav ul {
                list-style-type: none;
                margin: 0;
                padding: 0;
                display: flex;
                justify-content: center;
            }
            nav ul li {
                padding: 10px 20px;
            }
            nav ul li a {
                color: white;
                text-decoration: none;
            }
            nav ul li a:hover {
                text-decoration: underline;
            }
            main {
                padding: 20px;
                margin-bottom: 60px;
            }
            .video-list {
                display: grid;
                grid-template-columns: repeat(auto-fit, minmax(300px, 1fr));
                gap: 20px;
            }
            .video-item {
                background-color: white;
                border-radius: 5px;
                box-shadow: 0 0 5px rgba(0, 0, 0, 0.1);
                padding: 15px;
            }
            .video-item a {
                color: #007BFF;
                text-decoration: none;
            }
            .video-item a:hover {
                text-decoration: underline;
            }
            .pagination {
                display: flex;
                justify-content: center;
                margin-top: 20px;
            }
            .pagination a {
                padding: 8px 12px;
                margin: 0 5px;
                background-color: white;
                border-radius: 5px;
                box-shadow: 0 0 5px rgba(0, 0, 0, 0.1);
                text-decoration: none;
                color: #007BFF;
            }
            .pagination a:hover {
                text-decoration: underline;
            }
            footer {
                background-color: #333;
                color: white;
                text-align: center;
                padding: 10px 0;
                position: fixed;
                bottom: 0;
                width: 100%;
            }
        </style>
    </head>
    <body>
        <header>
            <h1>B 站热门视频导航 - 第 <?php echo $page; ?> 页</h1>
        </header>
        <nav>
            <ul>
                <li><a href="#">首页</a></li>
                <li><a href="#">热门视频</a></li>
            </ul>
        </nav>
        <main>
            <div>
                <?php
                foreach ($videos as $video) {
                    $title = $video['title'];
                    $bvid = $video['bvid'];
                    echo '<div>';
                    echo "<a href='video_pages/$bvid.html'>$title</a>";
                    echo '</div>';
                }
                ?>
            </div>
            <div>
                <?php
                if ($page > 1) {
                    echo '<a href="?page='. ($page - 1). '">上一页</a>';
                }
                echo '<a href="?page='. ($page + 1). '">下一页</a>';
                ?>
            </div>
        </main>
        <footer>
            <p>版权所有 &copy; 2025 你的网站名称</p>
        </footer>
    </body>
    </html>
    <?php
} else {
    echo "请求失败,错误信息: ". ($data ? $data['message'] : '未知错误');
}
?>

只要点一次自动生成一堆页面,只要有蜘蛛来访问就会生成!