Return to Snippet

Revision: 49665
at August 8, 2011 08:12 by peckham


Updated Code
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;
		$idStarts = strpos($value, "?v=");
		if ($idStarts === FALSE) {
			$idStarts = strpos($value, "&v=");
		}
		if ($idStarts === FALSE) {
			$idStarts = strpos($value, "/v/");
		}
		if ($idStarts === FALSE) {
			$idStarts = strpos($value, "#!v=");
			$idOffset = 4;
		}
		if ($idStarts === FALSE) {
			$idStarts = strpos($value, "youtu.be/");
			$idOffset = 9;
		}
		if ($idStarts !== FALSE) {
                        //there is a videoID present, now validate it
			$videoID = substr($value, $idStarts + $idOffset, $idLength);
			$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?
			if (substr($code, 0, 1) == 2) {
				$isValid = true;
			}
		}
	}
	return $isValid;
}

Revision: 49664
at July 27, 2011 23:55 by peckham


Initial Code
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;
		$idStarts = strpos($value, "?v=");
		if ($idStarts === FALSE) {
			$idStarts = strpos($value, "&v=");
		}
		if ($idStarts === FALSE) {
			$idStarts = strpos($value, "/v/");
		}
		if ($idStarts === FALSE) {
			$idStarts = strpos($value, "#!v=");
			$idOffset = 4;
		}
		if ($idStarts === FALSE) {
			$idStarts = strpos($value, 'youtu.be/');
			$idStarts = 9;
		}
		if ($idStarts !== FALSE) {
                        //there is a videoID present, now validate it
			$videoID = substr($value, $idStarts + $idOffset, $idLength);
			$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?
			if (substr($code, 0, 1) == 2) {
				$isValid = true;
			}
		}
	}
	return $isValid;
}

Initial URL


Initial Description
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.

Initial Title
Validate YouTube Video

Initial Tags
video

Initial Language
PHP