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?
I made a function to only fetch existing images from YouTube
function youtube_image($id) {
$resolution = array (
'maxresdefault',
'sddefault',
'mqdefault',
'hqdefault',
'default'
);
for ($x = 0; $x < sizeof($resolution); $x++) {
$url = '//img.youtube.com/vi/' . $id . '/' . $resolution[$x] . '.jpg';
if (get_headers($url)[0] == 'HTTP/1.0 200 OK') {
break;
}
}
return $url;
}