How do I get a YouTube video thumbnail from the YouTube API?

后端 未结 30 2755
Happy的楠姐
Happy的楠姐 2020-11-21 07:06

If I have a YouTube video URL, is there any way to use PHP and cURL to get the associated thumbnail from the YouTube API?

30条回答
  •  误落风尘
    2020-11-21 07:46

    A simple PHP function I created for the YouTube thumbnail and the types are

    • default
    • hqdefault
    • mqdefault
    • sddefault
    • maxresdefault

     

    function get_youtube_thumb($link,$type){
    
        $video_id = explode("?v=", $link);
    
        if (empty($video_id[1])){
            $video_id = explode("/v/", $link);
            $video_id = explode("&", $video_id[1]);
            $video_id = $video_id[0];
        }
        $thumb_link = "";
    
        if($type == 'default'   || $type == 'hqdefault' ||
           $type == 'mqdefault' || $type == 'sddefault' ||
           $type == 'maxresdefault'){
    
            $thumb_link = 'http://img.youtube.com/vi/'.$video_id.'/'.$type.'.jpg';
    
        }elseif($type == "id"){
            $thumb_link = $video_id;
        }
        return $thumb_link;}
    

提交回复
热议问题