/ Published in: PHP
This function validates the URL of a YouTube video. It supports four kinds of URL: ?v=, /v/, #!v=, and the short version (youtu.be).
To validate the ID a request to YouTube is made, this is done using a personal class utilizing cURL.
To validate the ID a request to YouTube is made, this is done using a personal class utilizing cURL.
Expand |
Embed | Plain Text
Copy this code and paste it in your HTML
public function isYoutubeVideo($value) { $isValid = false; //validate the url, see: http://snipplr.com/view/50618/ if (isValidURL($value)) { //code adapted from Moridin: http://snipplr.com/view/19232/ $idLength = 11; $idOffset = 3; if ($idStarts === FALSE) { } if ($idStarts === FALSE) { } if ($idStarts === FALSE) { $idOffset = 4; } if ($idStarts === FALSE) { $idOffset = 9; } if ($idStarts !== FALSE) { //there is a videoID present, now validate it $http = new HTTP("http://gdata.youtube.com"); $result = $http->doRequest("/feeds/api/videos/".$videoID, "GET"); //returns Array('headers' => Array(), 'body' => String); $code = $result['headers']['http_code']; //did the request return a http code of 2xx? $isValid = true; } } } return $isValid; }