Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

youssefboudaya's avatar

How to get video id and thumbnail of youtube playlist?

I have a module of youtube videos in my website with two function to show thumbnail of the youtube video and get it's id to play it. It works fine with normal youtube video url. These are my two functions:

public static function get_youtube_video_image($youtube_code)
{
    parse_str( parse_url( $youtube_code, PHP_URL_QUERY ), $my_array_of_vars );
    $id =  $my_array_of_vars['v'];

    $resolution = array (
    'maxresdefault',
    'hqdefault',
    'mqdefault',
    'sddefault',
    'default'
    );

    for ($x = 0; $x < sizeof($resolution); $x++) {
        $url = 'https://img.youtube.com/vi/' . $id . '/' . $resolution[$x] . '.jpg';

        if (get_headers($url)[0] == 'HTTP/1.0 200 OK') {
            break;
        }
    }
    return $url;  
}

public static function getYoutubeId($youtube_code)
{
    parse_str( parse_url( $youtube_code, PHP_URL_QUERY ), $my_array_of_vars );
    return  $my_array_of_vars['v'];
}

The problem is when i insert a url of youtube playlist "https://www.youtube.com/playlist?list=....." I get errors like:undefined index v. Is there a workaround with youtube playlist or not possible to do so ?

0 likes
4 replies
martinbean's avatar

@youssefboudaya You want to use oEmbed to get information on a video or playlist in a consistent manner.

You pass a URL to https://www.youtube.com/oembed and it will return details as JSON.

Single video

https://www.youtube.com/oembed?url=https%3A//youtube.com/watch%3Fv%3DM3r2XDceM6A&format=json

{
    "title": "Amazing Nintendo Facts",
    "author_name": "ZackScott",
    "author_url": "https://www.youtube.com/c/ZackScott",
    "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/M3r2XDceM6A/hqdefault.jpg",
    "html": "\u003ciframe width=\u0022200\u0022 height=\u0022113\u0022 src=\u0022https://www.youtube.com/embed/M3r2XDceM6A?feature=oembed\u0022 frameborder=\u00220\u0022 allow=\u0022accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture\u0022 allowfullscreen\u003e\u003c/iframe\u003e"
}

Playlist

https://youtube.com/oembed?url=https%3A//www.youtube.com/playlist%3Flist%3DPL4o29bINVT4EG_y-k5jGoOu3-Am8Nvi10&format=json

{
    "title": "POP Music Playlist 2021",
    "author_name": "POP Music Playlists - Billboard Charts \u0026 Top Songs",
    "author_url": "/c/POPMusicPlaylistsBillboardChartsTopSongs",
    "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/SlPhMPnQ58k/hqdefault.jpg",
    "html": "\u003ciframe width=\u0022200\u0022 height=\u0022113\u0022 src=\u0022https://www.youtube.com/embed/videoseries?list=PL4o29bINVT4EG_y-k5jGoOu3-Am8Nvi10\u0022 frameborder=\u00220\u0022 allow=\u0022accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture\u0022 allowfullscreen\u003e\u003c/iframe\u003e"
}
2 likes
youssefboudaya's avatar

This is very helpful thank you. I was wondering can i get the id of the video in the response ?

Please or to participate in this conversation.