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

后端 未结 30 2737
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:41

    // Get image form video URL
    $url = $video['video_url'];
    
    $urls = parse_url($url);
    
    //Expect the URL to be http://youtu.be/abcd, where abcd is the video ID
    if ($urls['host'] == 'youtu.be') :
    
        $imgPath = ltrim($urls['path'],'/');
    
    //Expect the URL to be http://www.youtube.com/embed/abcd
    elseif (strpos($urls['path'],'embed') == 1) :
    
        $imgPath = end(explode('/',$urls['path']));
    
    //Expect the URL to be abcd only
    elseif (strpos($url,'/') === false):
    
        $imgPath = $url;
    
    //Expect the URL to be http://www.youtube.com/watch?v=abcd
    else :
    
        parse_str($urls['query']);
    
        $imgPath = $v;
    
    endif;
    

提交回复
热议问题