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

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

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

14条回答
  •  情歌与酒
    2020-11-27 07:11

    Here is a solution that doesn't involve using youtube api, it checks if the video id exists when the url is loaded

    function checkYoutubeUrlIsValid($url) {
        $buffer = file_get_contents($url);
        $matches = [];
        preg_match('#[a-zA-Z0-9_-]{11}$#', $url, $matches);
    
        return strpos($buffer, $matches[0]) !== false;
    }
    

    Hope that helps

提交回复
热议问题