How do I check if a video exists on YouTube, using PHP?

Viewed 48399

How do I check if a video exists on YouTube, using PHP?

15 Answers

Here is the solution that I use to check if YouTube video exists using video id. This is C# code, but basically you can check if the thumbnail of the video exists, you will either get 200 or 404 which is very convenient.

    private async Task<bool> VideoExists(string id)
    {
        var httpClient = new HttpClient();
        var video = await httpClient.GetAsync($"https://img.youtube.com/vi/{id}/0.jpg");
        return video.IsSuccessStatusCode;
    }

A new way to get a YouTube video data after September 2021, is by cURL in PHP:

function getYouTubeData($videoId) {
    $theURL = "https://www.youtube.com/oembed?url=http://www.youtube.com/watch?v=$videoId&format=json";

    $curl = curl_init($theURL);
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);

    $body = curl_exec($curl);
    curl_close($curl);

    return json_decode($body, true);
}

Usage:

$ytData = getYouTubeData($video_id);

if (empty($ytData)) {
    $error = 'YouTube movie data could not be fetched.';
}
$title = $ytData['title'];

Sample output:

Array
(
    [title] => Use online tools available at Laminas Starter Kit - Laminas MVC
    [author_name] => Divix
    [author_url] => https://www.youtube.com/channel/UC6lBQpNdQH6cu0j15qhkCAg
    [type] => video
    [height] => 113
    [width] => 200
    [version] => 1.0
    [provider_name] => YouTube
    [provider_url] => https://www.youtube.com/
    [thumbnail_height] => 360
    [thumbnail_width] => 480
    [thumbnail_url] => https://i.ytimg.com/vi/LjDdAcB9-Mo/hqdefault.jpg
    [html] => <iframe width="200" height="113" src="https://www.youtube.com/embed/LjDdAcB9-Mo?feature=oembed" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe>
)

Found this solution on github: Check if youtube video exists

Easy to use:

$headers = get_headers('http://www.youtube.com/oembed?url=http://www.youtube.com/watch?v=nonexistingid');

if (!strpos($headers[0], '200')) {
    echo "The YouTube video you entered does not exist";
}

Working fine.

Here is a solution that doesn't involve using youtube api, it checks if the video id exists when the url is loaded

function checkYoutubeUrlIsValid($url) {
    $buffer = file_get_contents($url);
    $matches = [];
    preg_match('#[a-zA-Z0-9_-]{11}$#', $url, $matches);

    return strpos($buffer, $matches[0]) !== false;
}

Hope that helps

Here's a quick simple faster solution using the HEAD request method.

function check_youtube_video_exists($video_url) {

    if (strpos($video_url, 'youtube.com') > 0 || strpos($video_url, 'youtu.be') > 0) {
        $video_url = 'https://www.youtube.com/oembed?url='. $video_url .'&format=json';
    }

    $headers = @get_headers($video_url);

    return (strpos($headers[0], '200') > 0) ? true : false;
}

Check your YouTube URL like so:

if (check_remote_video_exists('YOUR_YOUTUBE_VIDEO_URL')) {
    // video exists, do stuff
} else {
    // video does not exist, do other stuff
}
Related