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

后端 未结 30 2662
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:36

    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;
    }
    

提交回复
热议问题