I'm attempting to assess the availability of an M3U8 or TS video via its URL using PHP. However, the current challenge is that the content of the stream is being requested, whereas my goal is to solely retrieve the status indicating whether the video is up or down.
Note: In cases where the video status is down, the response indicates that there is no content to load. However, when content is present, the current issue lies in the automatic loading of the content rather than simply obtaining the status.
Code I have been using:
<?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.";
}
?>
use cURL option CURLOPT_NOBODY
. By setting this to true
you will fetch only the headers and not the content.
Full example:
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.";
}