How do I check if a video exists on YouTube, using PHP?

后端 未结 14 1684
[愿得一人]
[愿得一人] 2020-11-27 06:40

How do I check if a video exists on YouTube, using PHP?

14条回答
  •  伪装坚强ぢ
    2020-11-27 07:19

    Youtube has support for the oEmbed format.
    Compared to the xml responsed provided by Pascal MARTIN, mine has only to download 600 bytes against 3800 bytes, making it faster and less bandwidth cosuming (only 1/6 of the size).

    function yt_exists($videoID) {
        $theURL = "http://www.youtube.com/oembed?url=http://www.youtube.com/watch?v=$videoID&format=json";
        $headers = get_headers($theURL);
    
        return (substr($headers[0], 9, 3) !== "404");
    }
    
    $id = 'yyDUC1LUXSU'; //Video id goes here
    
    if (yt_exists($id)) {
        //  Yep, video is still up and running :)
    } else {
        //  These aren't the droids you're looking for :(
    }
    

提交回复
热议问题