如果视频不存在,则仅从数据库中输出无效的 YouTube ID

Only output invalid YouTube ID's from database if video doesn't exists

我的数据库中存储了 YouTube 视频 ID。我正在尝试输出仅无效的 ID。我正在使用 get_headers / oembed,它允许我检查 YouTube 上是否存在视频。然后我循环遍历 ID。这目前正在运行,但它显示了我的 table 中的所有 YouTube ID,然后将文本 "is invalid" 添加到无效的 ID。我只需要显示那些无效的 - 没有别的!

如果有人不介意的话,我需要一些帮助。非常感谢。

代码:

$servername = "localhost";
$username = "";
$password = "";
$dbname = "";

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
   die("Connection failed: " . $conn->connect_error);
}

$sql = "SELECT src_id FROM youtube_videos ";
$result = $conn->query($sql);

while($row = $result->fetch_assoc()) {
    echo 'Video ID: '.$row["src"];
    $headers = get_headers('http://www.youtube.com/oembed?url=http://www.youtube.com/watch?v='.$row["src_id"].'');
    if (!strpos($headers[0], '200')) {
        echo " is invalid";
    } else {
        echo "";
    }

    echo 'no results';

}

如果 header 代码不是 200,就打印视频 ID?

while ($row = $result->fetch_assoc()) {
    $headers = get_headers('http://www.youtube.com/oembed?url=http://www.youtube.com/watch?v='.$row["src_id"].'');
    if (!strpos($headers[0], '200')) {
        echo "Video ID: ".$row['src']." is invalid\n";
    }
}

可能还想研究一种更好的抓取方式 response-headers,这种方式可能并非对所有情况都 100% 准确。我建议使用类似

while ($row = $result->fetch_assoc()) {
    $headers = get_headers('http://www.youtube.com/oembed?url=http://www.youtube.com/watch?v='.$row["src_id"].'');
    if (substr($headers[0], 9, 3) != 200) {
        echo "Video ID: ".$row['src']." is invalid\n";
    }
}