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

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

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

相关标签:
14条回答
  • 2020-11-27 07:00

    You want to validate if a youtube url is an url to a real youtube video? This is quite hard, you could use regular expressions, but keep in mind that there are loads of valid ways to express a youtube url:

    • http://www.youtube.com/watch?v=p72I7g-RXpg
    • http://www.youtube.com/watch?asv=76621-2&v=p72I7g-RXpg
    • http://www.youtube.com/v/RdPxlTX27Fk
    • etc.

    Also the video code can contain alphanumeric characters, underscores, -characters (dunno what they are called) and possibly more.

    0 讨论(0)
  • 2020-11-27 07:03

    Here's a quick simple faster solution using the HEAD request method.

    function check_youtube_video_exists($video_url) {
    
        if (strpos($video_url, 'youtube.com') > 0 || strpos($video_url, 'youtu.be') > 0) {
            $video_url = 'https://www.youtube.com/oembed?url='. $video_url .'&format=json';
        }
    
        $headers = @get_headers($video_url);
    
        return (strpos($headers[0], '200') > 0) ? true : false;
    }
    

    Check your YouTube URL like so:

    if (check_remote_video_exists('YOUR_YOUTUBE_VIDEO_URL')) {
        // video exists, do stuff
    } else {
        // video does not exist, do other stuff
    }
    
    0 讨论(0)
  • 2020-11-27 07:06

    Another (kind of inefficient) way is to use cURL to get the HTML of the supposed video page and run some regular expressions to verify that it's an actual video page.

    0 讨论(0)
  • 2020-11-27 07:07

    Found this solution on github: Check if youtube video exists

    Easy to use:

    $headers = get_headers('http://www.youtube.com/oembed?url=http://www.youtube.com/watch?v=nonexistingid');
    
    if (!strpos($headers[0], '200')) {
        echo "The YouTube video you entered does not exist";
    }
    

    Working fine.

    0 讨论(0)
  • 2020-11-27 07:08

    I used the YouTube API for checking if a video exists on You Tube. I downloaded the Google API Client Library for PHP. I used the following function:

    /**
    * Used to check if the given movie is availabe on youtube
    *
    * It uses youtube api and checks if given movie is available on youtube
    * If a movie is not available then it returns false
    *
    * @param string $youtube_video_url the youtube movie url
    *
    * @return boolean $is_available indicates if the given video is available on youtube
    */
    private function IsMovieAvailable($youtube_video_url)
    {
        /** The autoload.php file is included */
        include_once("autoload.php");
    
        /** Is available is set to false */
        $is_available = false;
    
        /** The youtube video id is extracted */
        $video_id = str_replace("https://www.youtube.com/watch?v=", "",   $youtube_video_url);
    
       $DEVELOPER_KEY = $google_api_key;
       $client = new \Google_Client();
       $client->setDeveloperKey($DEVELOPER_KEY);
    
       // Define an object that will be used to make all API requests.
       $youtube = new \Google_Service_YouTube($client);
    
       // Call the search.list method to retrieve results matching the specified
       // query term.
       $searchResponse = $youtube->videos->listVideos('status', array('id' => $video_id));
    
       /** Each item in the search results is checked */
       foreach ($searchResponse['items'] as $video) {
           /** If the video id matches the given id then function returns true  */
           if ($video['id'] == $video_id) {
               $is_available = true;
               break;
           }
        }
        return $is_available;
    }
    
    0 讨论(0)
  • 2020-11-27 07:10

    Request the URLs with the HEAD method, like so:

    HEAD /watch?v=p72I7g-RXpg HTTP/1.1
    Host: www.youtube.com                         
    
    HTTP/1.1 200 OK
    [SNIP]
    
    
    HEAD /watch?v=p72I7g-BOGUS HTTP/1.1
    Host: www.youtube.com              
    
    HTTP/1.1 303 See Other
    [SNIP]
    Location: http://www.youtube.com/index?ytsession=pXHSDn5Mgc78t2_s7AwyMvu_Tvxn6szTJFAbsYz8KifV-OP20gt7FShXtE4gNYS9Cb7Eh55SgoeFznYK616MmFrT3Cecfu8BcNJ7cs8B6YPddHQSQFT7fSIXFHd5FmQBk299p9_YFCrEBBwTgtYhzKL-jYKPp2zZaACNnDkeZxCr9JEoNEDXyqLvgbB1w8zgOjJacI4iIS6_QvIdmdmLXz7EhBSl92O-qHOG9Rf1HNux_xrcB_xCAz3P3_KbryeQk_9JSRFgCWWgfwWMM3SjrE74-vkSDm5jVRE3ZlUI6bHLgVb7rcIPcg
    
    0 讨论(0)
提交回复
热议问题