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

后端 未结 30 2794
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条回答
  •  猫巷女王i
    2020-11-21 07:43

    Another good alternative would be to use the oEmbed API which is supported by YouTube.

    You simply add your YouTube URL to the oEmbed URL and you'll receive a JSON including a thumbnail and the HTML code for embedding.

    Example:

    http://www.youtube.com/oembed?format=json&url=http%3A//youtube.com/watch%3Fv%3DxUeJdWYdMmQ
    

    Would give you:

    {
      "height":270,
      "width":480,
      "title":"example video for 2020",
      "thumbnail_width":480,
      "html":"...",
      "thumbnail_height":360,
      "version":"1.0",
      "provider_name":"YouTube",
      "author_url":"https:\/\/www.youtube.com\/channel\/UCza6VSQUzCON- AzlsrOLwaA",
      "thumbnail_url":"https:\/\/i.ytimg.com\/vi\/xUeJdWYdMmQ\/hqdefault.jpg",
      "author_name":"Pokics",
      "provider_url":"https:\/\/www.youtube.com\/",
      "type":"video"
    }
    

    Read the documentation for more information.

提交回复
热议问题