Page 1 of 1

youtube embed code validation

Posted: Wed Oct 19, 2011 10:56 pm
by keiferhelms
I am a beginner and I have a script written by someone else. this one part dosent work right because youtube has changed the format of thier embed code.

It is a form where the user enters a video title, youtube embed code, and description. They enter the info then i beleive it extracts the info needed and puts it in my datsbase. The current code will validate the old embed code that looks like this

Code: Select all

<object width="425" height="350"><param name="movie" value="http://www.youtube.com/v/7_6B6vwE83U"></param>
<embed src="http://www.youtube.com/v/7_6B6vwE83U" type="application/x-shockwave-flash"
width="425" height="350"></embed></object>
I need it to validate the new embed code that looks like this

Code: Select all

<iframe width="560" height="315" src="http://www.youtube.com/embed/v3vbg9eLDmE" frameborder="0" 
allowfullscreen></iframe>
Right now when you input the iframe code is gives the error embed code required.

The current PHP code is as follows

Code: Select all

		//Validate Fields
		if(trim($postData['title'])==''?TRUE:FALSE)
		{
			$mainframe->enqueueMessage(JText::_('VIDEO TITLE REQUIRED'), 'error');
			$isValid = false;			
		} //End Of Title Validation
	
		if(trim($postData['embedCode'])==''?TRUE:FALSE)
		{
			$mainframe->enqueueMessage(JText::_('EMBED CODE REQUIRED'), 'error');
			$isValid = false;
		} else {
			//Check Embed Code
			$embedCode = $postData['embedCode'];	

			
			
			/*if(isset($matches[1]))
			{*/
				preg_match('/<param (.*)">/i', $embedCode, $matches);
				if(isset($matches[1]))
				{
					if(str_replace('movie','as',$matches[1]))
					{
						$val=explode('>', $matches[1]);
						$str=str_replace('name','',$val[0]);
						$str=str_replace('=','',$str);
						$str=str_replace('"','',$str);
						$str=str_replace('value','',$str);$str=str_replace('movie','',$str);
						$str=str_replace('http://www.youtube.com/v/','',$str);
						$str=trim($str);
						$all=$this->GetYouTubeVideoURL($str);    // GET MEDIA URL
						$all_infos=$this->GrabYouTube("http://www.youtube.com/watch?v=$str"); //GET THUMBNAIL URL
						$postData['youtube_video_id'] = $all_infos['youtube_video_id'];
						$postData['image_url'] 		= $all_infos['thumbnail_url'];
					
					} else {

						$mainframe->enqueueMessage(JText::_('EMBED CODE ERROR'), 'error');
						$isValid = false;
					}
				} else {

					$mainframe->enqueueMessage(JText::_('EMBED CODE ERROR'), 'error');
					$isValid = false;
				}
			/*} else {
			
					$mainframe->enqueueMessage(JText::_('EMBED CODE ERROR'), 'error');
					$isValid = false;
			}*/
			
			
			
		}//End Of Embed Code Validation

		return $isValid;
furthur down in the script is the GetYouTubeVideoURL and GrabYouTube functions mentioned in the php code above. I think they get the video description and stuff and the video thumbnail. Im not sure if those work because i cant get past the validation but i will put them below

Code: Select all

function GetYouTubeVideoURL($youtube_video_id)
	{
	    $youtube_page = $this->_GetPage('http://www.youtube.com/watch?v='.$youtube_video_id);
	    $youtube_video_url = '';
	    if ($youtube_page) {
	        preg_match('/watch_fullscreen\?video_id=(.*?)&l=(.*?)+&t=(.*?)&/', $youtube_page, $matches);
	        if ($matches && isset($matches[1]) && isset($matches[3])) {
	            $youtube_t = $matches[3];
	            // video...
	            $youtube_video_url = 'http://www.youtube.com/get_video?video_id=' . $youtube_video_id . '&t=' . $youtube_t;
	        }
	    }
	    return $youtube_video_url;
	} // End of GetYouTubeVideoURL Function
	
	function GrabYouTube($youtube_url)
	{
	    $youtube_details_arr = array('youtube_video_id' => '','title' => '','description' => '','tags' => '','error' => '');
    		$parse_value_arr = parse_url($youtube_url);
		parse_str($parse_value_arr['query'], $youtube_url_prop);
		$youtube_video_id = isset($youtube_url_prop['v']) ? $youtube_url_prop['v'] : '';
		if($youtube_video_id)
		{
			$youtube_details_arr['youtube_video_id'] = $youtube_video_id;
			$youtube_xml = $this->_GetPage('http://www.youtube.com/api2_rest?method=youtube.videos.get_details&dev_id=rG48P7iz0eo&video_id='.$youtube_video_id);
			preg_match('/<title>(.*)<\/title>/i', $youtube_xml, $matches);
			if (isset($matches[1]))
			   $youtube_details_arr['title'] = $matches[1];
			preg_match('/<description>(.*?)<\/description>/is', $youtube_xml, $matches);
			if (isset($matches[1]))
			   $youtube_details_arr['description'] = trim($matches[1]);
			preg_match('/<tags>(.*?)<\/tags>/is', $youtube_xml, $matches);
			if (isset($matches[1]))
			   $youtube_details_arr['tags'] = trim($matches[1]);
		    preg_match('/<thumbnail_url>(.*?)<\/thumbnail_url>/is', $youtube_xml, $matches);
		    if (isset($matches[1])) {
		       $thumbnail_url = trim($matches[1]);
	   	   }
		 // new lines for youtube thumbnail
		 
		 $splitEmbed = explode('?',$youtube_video_id);
		 $youtubeid = $splitEmbed[0];
		 $thumbnail_url='http://i4.ytimg.com/vi/'.$youtubeid.'/default.jpg';
		 $youtube_details_arr['thumbnail_url']=$thumbnail_url;

		 } else {
		 	$youtube_details_arr['error'] = 'Couldn\'t find YouTube Video ID.';					
		 }		 
		    return $youtube_details_arr;
	 } // End of GrabYouTube Function