How to Download Video from Youtube online using Php Script

Authors: CodeToday | PHP Code | Views: 607 | Posted: 04 AM: 09/14/2017

In the normal way, if you want to download videos from youtube you need software or add on the browser, today I will guide the download direct link using the php script

To get the download link from youtube video, you do not need to have youtube API key, you just need Video ID

Ex: https://www.youtube.com/watch?v=4rb8aOzy9t4, Video ID: 4rb8aOzy9t4

Link to get information from youtube below:

http://www.youtube.com/get_video_info?&video_id=4rb8aOzy9t4&asv=3&el=detailpage&hl=en_US

You can change the Video ID depending on the video you want to download. After running the link above, the results shown below:



You can use the php script to analyze this data like the code below

PHP Script Source Code

<?
$url     = 'http://www.youtube.com/get_video_info?&video_id=4rb8aOzy9t4&asv=3&el=detailpage&hl=en_US';
$data    = curlGet($url);
$getInfo = urldecode($data); 

$arrayInfo = explode("&",str_replace(',','&',$getInfo));

echo '<ul>';

foreach($arrayInfo as $info) { $i++;

   if(stristr($info,"googlevideo.com") == true ) { 
   
      $info = str_replace(',','&',$info);
	  parse_str($info);
	  $linkVideo = str_replace(array(',','?'),array('&','?type=&'),$url);	  // Fix for get Mime		    
		 
	      if(stristr($linkVideo,"youtube.com") == false)  {
		        $link = $linkVideo.'&title='. urlencode($title);				
				echo '<li>'.$link.'</li>';				
           }		   				   
	}		
}
echo '</ul>';	

function curlGet($url){
	$ch = curl_init();
	curl_setopt($ch, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']);
	curl_setopt($ch, CURLOPT_HTTPGET, 1 );
	curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1 );
	curl_setopt($ch, CURLOPT_FOLLOWLOCATION , 1 );
	curl_setopt($ch, CURLOPT_FOLLOWLOCATION , 1 );
	curl_setopt($ch, CURLOPT_URL, $url );
	curl_setopt($ch, CURLOPT_REFERER, $ref );
	curl_setopt($ch, CURLOPT_COOKIEJAR, 'cookie.txt');
	curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);     
	curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
	$data = curl_exec($ch);
	curl_close($ch);
	return $data;
}
?>

Below is the result obtained when running the code above, with this result you can display and filter according to the format you want to display.

How to Download Video from Youtube online using Php Script


If you have any questions, please leave a message below