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

后端 未结 30 2756
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

    Method 1:

    You can find all information for a YouTube video with a JSON page which has even "thumbnail_url", http://www.youtube.com/oembed?format=json&url={your video URL goes here}

    Like final URL look + PHP test code

    $data = file_get_contents("https://www.youtube.com/oembed?format=json&url=https://www.youtube.com/watch?v=_7s-6V_0nwA");
    $json = json_decode($data);
    var_dump($json);
    

    Output

    object(stdClass)[1]
      public 'width' => int 480
      public 'version' => string '1.0' (length=3)
      public 'thumbnail_width' => int 480
      public 'title' => string 'how to reminder in window as display message' (length=44)
      public 'provider_url' => string 'https://www.youtube.com/' (length=24)
      public 'thumbnail_url' => string 'https://i.ytimg.com/vi/_7s-6V_0nwA/hqdefault.jpg' (length=48)
      public 'author_name' => string 'H2 ZONE' (length=7)
      public 'type' => string 'video' (length=5)
      public 'author_url' => string 'https://www.youtube.com/channel/UC9M35YwDs8_PCWXd3qkiNzg' (length=56)
      public 'provider_name' => string 'YouTube' (length=7)
      public 'height' => int 270
      public 'html' => string '' (length=171)
      public 'thumbnail_height' => int 360
    

    For details, you can also see How to get a YouTube video thumbnail using id or https://www.youtube.com/watch?v=mXde7q59BI8 video tutorial 1

    Method 2:

    Using YouTube image link, https://img.youtube.com/vi/"insert-youtube-video-id-here"/default.jpg

    Method 3:

    Using browser source code for getting thumbnail using video URL link -go to video source code and search for thumbnailurl. Now you can use this URL into your source code:

    {img src="https://img.youtube.com/vi/"insert-youtube-video-id-here"/default.jpg"}
    

    For details you can also see How to get a YouTube video thumbnail using id or https://www.youtube.com/watch?v=9f6E8MeM6PI video tutorial 2

提交回复
热议问题