tags:

views:

1110

answers:

4

Hi all

Could anyone kindly tell me how to validate the YOUTUBE URL. using PHP

Example: For this URL how can we Validate: www.youtube.com/watch?v=GeppLPQtihA

Thanks in Advance..

FEro

A: 

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:

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

Pim Jager
+1  A: 

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
+10  A: 

Hi,

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 : YouTube APIs and Tools - Developer's Guide: PHP


The Retrieving a specific 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.

Pascal MARTIN
A: 

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