我正在尝试使用 simple_html_dom 提取 youtube 链接。 我设法用 simple_html_dom.php 提取数据。我得到视频 URL、缩略图、标题。标签等..提取。我还可以在“从数据库到 post.php 页面”上显示它们。我在数据库中只有一个表(videos_yt)和 2 列( ID , [yt_link] )。
post.php
<?php
//////////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////- Database -//////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////
$conn = mysqli_connect("localhost", "root", "", "video_platform");
$id = $_GET['id'];
$query = "SELECT id, title, video, duration, views FROM videos WHERE id = $id";
$result = mysqli_query($conn, $query);
$video = mysqli_fetch_assoc($result);
if (isset($_GET['id'])) {
$id = $_GET['id'];
// Erhöhen Sie die Ansichten für das aktuelle Video
$update_views_query = "UPDATE videos SET views = views + 1 WHERE id = $id";
mysqli_query($conn, $update_views_query);
}
//////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////- VIDEO -/////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////////
$url = $video['video'];
$content = file_get_contents($url);
$first_step = explode( '<main id="container">' , $content );
$second_step = explode('<div id="video_container">' , $first_step[1] );
$ds = explode('<script type="application/ld+json">' , $second_step[0] );
echo "<center>";
preg_match_all('!https?://\S+!', $ds[0], $matches);
$all_urls = $matches[0];
echo "</div>";
//////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////- Title -/////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////////
// Die URL der Webseite, von der Daten extrahiert werden sollen
$url = $video['video'];
suchen
$element = $html->find('h1', 0);
// Den Wert des 'name'-Attributs auslesen
$name = $element->getAttribute('name');
//////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////- thumb -/////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////////
$container = $html->find('.play_cover', 0);
$image = $container->find('img', 0);
$src = $image->getAttribute('src');
$url = parse_url($src);
$imagePath = $url['path'];
?>
但我想在起始页上有完全相同的过程。 我还希望它从数据库中获取各个链接并使用 DOM 在网格中显示它们!
实现这个的最好方法是什么。 `
` index.php
<?php
// Verbindung zur Datenbank herstellen
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "video_platform";
// Verbindung herstellen
$conn = mysqli_connect($servername, $username, $password, $dbname);
// Überprüfen, ob die Verbindung erfolgreich hergestellt wurde
if (!$conn) {
die("Verbindung fehlgeschlagen: " . mysqli_connect_error());
}
// Abfrage für die Videos
$sql = "SELECT title, thumbnail, video, duration FROM videos";
$result = mysqli_query($conn, $sql);
// Überprüfen, ob die Abfrage erfolgreich ausgeführt wurde
if (mysqli_num_rows($result) > 0) {
// Daten für jedes Video in einem Array speichern
$videos = array();
while ($row = mysqli_fetch_assoc($result)) {
$videos[] = $row;
}
} else {
echo "Keine Daten gefunden";
}
// Verbindung schließen
mysqli_close($conn);
?>
<!DOCTYPE html>
<html>
<head>
<link href="https://vjs.zencdn.net/7.15.4/video-js.css" rel="stylesheet" />
<script src="https://vjs.zencdn.net/7.15.4/video.js"></script>
<link rel="stylesheet" type="text/css" href="all.css">
<title>Discord Video-Plattform</title>
</head>
<body>
<h1>Discord Video-Plattform</h1>
<div class="video-grid">
<?php
// Hier können Sie den Code für das Abrufen der Videos aus der Datenbank verwenden
$conn = mysqli_connect("localhost", "root", "", "video_platform");
$query = "SELECT id, title, thumbnail, duration, views, likes FROM videos";
$result = mysqli_query($conn, $query);
while ($video = mysqli_fetch_assoc($result)) {
echo '<a href="post.php?id=' . $video['id'] . '">';
echo ' <div class="video-thumbnail">';
echo ' <img src="' . $video['thumbnail'] . '" alt="' . $video['title'] . '">';
echo ' <div class="video-statistics">';
echo '<p class="views">' . $video['views'] . ' views</p>';
echo ' <p class="duration">' . $video['duration'] . '<img alt="clockicon" src="images/clock.png" style="width:12px; width: 14px;
padding-right: 3px;
margin-top: -2px;">
</p>';
echo ' </div>';
echo ' </div>';
echo ' <div class="video-bottomdesc">';
echo ' <p class="titlecontent">' . $video['title'] . '</p>';
echo ' </div>';
echo '</a>';
}
?>
<!-- Like-Schaltfläche anzeigen -->
</div>
</body>
</html>