如何使用视频的 URL 验证视频的状态,而不用 PHP 实际加载其内容?

问题描述 投票:0回答:1

我正在尝试使用 PHP 通过 URL 评估 M3U8 或 TS 视频的可用性。然而,当前的挑战是正在请求流的内容,而我的目标是仅检索指示视频是向上还是向下的状态。

注意:如果视频状态为down,则响应指示没有内容可加载。然而,当内容存在时,当前的问题在于内容的自动加载,而不是简单地获取状态。

我一直在使用的代码:

<?php
    function isVideoUp($url) {
        $ch = curl_init($url);
        
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_HEADER, true);

        $response = curl_exec($ch);
        
        $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
        
        curl_close($ch);

        return ($httpCode >= 200 && $httpCode < 300);
    }

    $url = 'http://example.com/test.m3u8';
    if (isVideoUp($url)) {
        echo "The video is up!";
    } else {
        echo "The video is down or inaccessible.";
    }
?>
php m3u8
1个回答
0
投票

使用 cURL 选项

CURLOPT_NOBODY
。通过将其设置为
true
,您将仅获取标题而不是内容。

完整示例:

function isVideoUp($url) {
    $ch = curl_init($url);
    
    curl_setopt($ch, CURLOPT_NOBODY, true); // fetch only headers
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_HEADER, true);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); // follow redirects

    curl_exec($ch);
    $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
    curl_close($ch);

    return ($httpCode >= 200 && $httpCode < 300);
}

$url = 'http://example.com/test.m3u8';
if (isVideoUp($url)) {
    echo "The video is up!";
} else {
    echo "The video is down or inaccessible.";
}

https://www.php.net/manual/en/function.curl-setopt.php

© www.soinside.com 2019 - 2024. All rights reserved.