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

后端 未结 30 2754
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:43

    You can get the video ID from the YouTube video url using parse_url ,parse_str and then insert in to the predictive urls for images. Thanks to YouTube for the predictive URLs

    $videoUrl = "https://www.youtube.com/watch?v=8zy7wGbQgfw";
    parse_str( parse_url( $videoUrl, PHP_URL_QUERY ), $my_array_of_vars );
    $ytID = $my_array_of_vars['v']; //gets video ID
    
    print "https://img.youtube.com/vi/$ytID/maxresdefault.jpg";
    print "https://img.youtube.com/vi/$ytID/mqdefault.jpg";
    print "https://img.youtube.com/vi/$ytID/hqdefault.jpg";
    print "https://img.youtube.com/vi/$ytID/sddefault.jpg";
    print "https://img.youtube.com/vi/$ytID/default.jpg";
    

    You can use this tool to generate YouTube thumbnails

    https://tools.tutsplanet.com/index.php/get-youtube-video-thumbnails

提交回复
热议问题