How to get a youtube playlist thumbnail?

后端 未结 3 1899
梦如初夏
梦如初夏 2021-02-20 00:09

Is there a way to get youtube playlist thumbnail like how you can get a thumbnail of a video, explained here: How do I get a YouTube video thumbnail from the YouTube API?

3条回答
  •  情歌与酒
    2021-02-20 00:41

    This is what worked for me. Similar to the other post regarding playlist items but I just changed the API call. Hope it helps.

    //get playlist itmes

    $data = file_get_contents("https://www.googleapis.com/youtube/v3/playlistItems?key=< YOUR API KEY >&part=snippet&playlistId="");
    

    //decode response from youtube

    $json = json_decode($data);
    

    //if you want to see the full response use print_r($json);

    //each items in the response corresponds to a video in the playlist so lets get the first video (items[0]) get its snippet and corresponding thumbnails and we will take the url for the default size. If you want the second video use items [1] etc.

    $video_thumbnail = $json->items[0]->snippet->thumbnails->default->url;
    

    //set the videos alt tag as the snippet description

    $video_alt = $json->items[0]->snippet->description; 
    

    //echo the image url into an image tag

    echo ''. $video_alt .'';
    

提交回复
热议问题