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?
相关问题
- Views base64 encoded blob in HTML with PHP
- Laravel Option Select - Default Issue
- PHP Recursively File Folder Scan Sorted by Modific
- Can php detect if javascript is on or not?
- Using similar_text and strpos together
Method 1:
you can find youtube video all information with 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
OutPut
for detail you can also see https://www.youtube.com/watch?v=mXde7q59BI8 video tutorial 1
Method 2: using youtube img 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 sorce Code: {img src="https://img.youtube.com/vi/"insert-youtube-video-id-here"/default.jpg"}
for detail you can also see http://hzonesp.com/php/get-youtube-video-thumbnail-using-id/ or https://www.youtube.com/watch?v=9f6E8MeM6PI video tutorial 2
In YouTube Data API v3, you can get video's thumbnails with the videos->list function. From snippet.thumbnails.(key), you can pick the default, medium or high resolution thumbnail, and get its width, height and URL.
You can also update thumbnails with the thumbnails->set functionality.
For examples, you can check out the YouTube API Samples project. (PHP ones.)
YouTube is owned by Google and Google likes to have a reasonable number of images for different screen sizes hence its images are stored in different sizes, here is an example of how your thumbnail like will he like
Low quality thumbnail:
Medium quality thumbnail:
High quality thumbnail:
Maximum quality thumbnail:
YouTube Data API
YouTube Provides us the 4 generated images for every video through the Data API (v3), For Ex -
https://i.ytimg.com/vi/V_zwalcR8DU/maxresdefault.jpg
https://i.ytimg.com/vi/V_zwalcR8DU/sddefault.jpg
https://i.ytimg.com/vi/V_zwalcR8DU/hqdefault.jpg
https://i.ytimg.com/vi/V_zwalcR8DU/mqdefault.jpg
Getting Access to the Images Via API
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 yourVideoId and yourApiKey to the your respective video-id and api-key and its response will be a JSON output providing you the 4links in the thumbnails of snippet variable (If all are available).
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
You can use this tool to generate YouTube thumbnails
https://codeatools.com/get-youtube-video-thumbnails