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

后端 未结 30 3287
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:31

    YouTube Data API

    YouTube provides us the four generated images for every video through the Data API (v3), for example,

    1. https://i.ytimg.com/vi/V_zwalcR8DU/maxresdefault.jpg

    2. https://i.ytimg.com/vi/V_zwalcR8DU/sddefault.jpg

    3. https://i.ytimg.com/vi/V_zwalcR8DU/hqdefault.jpg

    4. https://i.ytimg.com/vi/V_zwalcR8DU/mqdefault.jpg

    Getting access to the images via the API

    1. First get your public API key at Google API Console.
    2. As per YouTube's thumbnail reference in the API documentation, you need to access the resources on snippet.thumbnails.
    3. As per this, you need to phrase your URL like this -

      www.googleapis.com/youtube/v3/videos?part=snippet&id=`yourVideoId`&key=`yourApiKey`
      

    Now change your video ID and your API key to the your respective video-id and api-key and its response will be a JSON output providing you the four links in the thumbnails of snippet variable (if all are available).

提交回复
热议问题