How to check if YouTube video exists?

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

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

  • Answer:

    What about using Youtube's API ? After all, that would mean using some official, which is less likely to change than going with parsing some HTML page. For more informations : http://code.google.com/apis/youtube/2.0/developers_guide_php.html The http://code.google.com/apis/youtube/2.0/developers_guide_php.html#Retrieving_Video_Entry seems quite interesting : if you send a request to an URL like this one : http://gdata.youtube.com/feeds/api/videos/videoID (Replacing "videoID" by the idea of the video, of course -- "GeppLPQtihA" in your example) You'll get some ATOM feed if the video is valid ; and "Invalid id" if it's not And, I insist : this way, you rely on a documented API, and not on some kind of behavior that exists today, but is not garanteed.

Fero at Stack Overflow Visit the source

Was this solution helpful to you?

Other answers

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

gnud

http://www.youtube.com/watch?v=bQVoAWSP7k4 http://www.youtube.com/watch?v=bQVoAWSP7k4&feature=popular http://www.youtube.com/watch?v=McNqjYiFmyQ&feature=related&bhablah http://youtube.com/watch?v=bQVoAWSP7k4 var matches = $('#videoUrl').val().match(/http:\/\/(?:www\.)?youtube.*watch\?v=([a-zA-Z0-9\-_]+)/); if (matches) { alert('valid'); } else { alert('Invalid'); }

mahesh

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.

Pim Jager

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.

Daniel S

Related Q & A:

Just Added Q & A:

Find solution

For every problem there is a solution! Proved by Solucija.

  • Got an issue and looking for advice?

  • Ask Solucija to search every corner of the Web for help.

  • Get workable solutions and helpful tips in a moment.

Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.