YouTube Video Downloader using PHP – This article share’s how to download the youtube videos using PHP script. Now a days most of peoples used youtube of their entertainment and someone else try to download the video. So we are program wise develop the youtube video downloader in PHP. YouTube video downloader using php very helpful who have knowledge about programming and even also helpful for students and others.

youtube video downloader using php

But still youtube not officially provide the download option, but we have no problem about it, because we have many resource to download the video in software tool or other way. But i think there is no option to download the video in program way so here i have launch youtube video downloader using php.

YouTube Video Downloader using PHP

Now time to create index.php file.

<?php 
include('header.php');
include_once("db_connect.php");
?>
<link rel="stylesheet" href="css/style.css" />
<title>YouTube video downloader using PHP</title>
<link href="css/bootstrap.min.css" rel="stylesheet" media="screen">
<body style="background-color: pink">
<div class="container">
	<h1 style="color:blue">YouTube video downloader using PHP</h1>
	<form class="form-download" method="get" id="download" action="getvideo.php">
		<input type="text" name="videoid" style="height:30px" id="videoid" size="40" placeholder="youtube video id only.." />
		<input class="btn btn-primary" style="background:#3399ff;height:30px;color:white" type="submit" name="type" id="type"value="Download" />
		<p style="font-weight: bold">Enter the video id only</p>
		<p style="font-weight: bold">for example, the basic url is: https://www.youtube.com/watch?v=5BG1-kZc_qE</p>
		<p style="font-weight: bold">you have copy the id only after v= <span style="color: red">(i.e just copy 5BG1-kZc_qE)</span>

		<?php
		include_once('config.php');
		function is_chrome(){
			$agent=$_SERVER['HTTP_USER_AGENT'];
			if( preg_match("/like\sGecko\)\sChrome\//", $agent) ){	// if user agent is google chrome
				if(!strstr($agent, 'Iron')) // but not Iron
					return true;
			}
			return false;
		}
		?>
	</form>	
	
</div>

Demo

Now build cURL to fetch the videos. many peoples are no idea about curl so first you have learn about curl and why we used that. In shortly the curl is

What is cURL

The cURL library (or, ‘libcurl’ which is the package name on the server) is often used in PHP to access data from outside web pages. Common uses include reading rss feeds or accessing third-party API systems. libcurl currently supports the http, https, ftp, gopher, telnet, dict, file, and ldap protocols.

read more about curl cURL Overview

now create curl.php

<?php

if ($config['multipleIPs'] === true) {

	$outgoing_ip = $config['IPs'][mt_rand(0, count($config['IPs']) - 1)];
}
 
function curlGet($URL) {
	global $config;
    $ch = curl_init();
    $timeout = 3;
    if ($config['multipleIPs'] === true) {
	    global $outgoing_ip;
	    curl_setopt($ch, CURLOPT_INTERFACE, $outgoing_ip);
	}
    curl_setopt( $ch , CURLOPT_URL , $URL );
    curl_setopt( $ch , CURLOPT_RETURNTRANSFER , 1 );
    curl_setopt( $ch , CURLOPT_CONNECTTIMEOUT , $timeout );
    $tmp = curl_exec( $ch );
    curl_close( $ch );
    return $tmp;
}  

function get_location($url) {
	global $config;
	$my_ch = curl_init();
	if ($config['multipleIPs'] === true) {
	    global $outgoing_ip;
	    curl_setopt($my_ch, CURLOPT_INTERFACE, $outgoing_ip);
	}
	curl_setopt($my_ch, CURLOPT_URL,$url);
	curl_setopt($my_ch, CURLOPT_HEADER,         true);
	curl_setopt($my_ch, CURLOPT_NOBODY,         true);
	curl_setopt($my_ch, CURLOPT_RETURNTRANSFER, true);
	curl_setopt($my_ch, CURLOPT_TIMEOUT,        10);
	$r = curl_exec($my_ch);
	 foreach(explode("\n", $r) as $header) {
		if(strpos($header, 'Location: ') === 0) {
			return trim(substr($header,10)); 
		}
	 }
	return '';
}

function get_size($url) {
	global $config;
	$my_ch = curl_init();
	if ($config['multipleIPs'] === true) {
	    global $outgoing_ip;
	    curl_setopt($my_ch, CURLOPT_INTERFACE, $outgoing_ip);
	}
	curl_setopt($my_ch, CURLOPT_URL,$url);
	curl_setopt($my_ch, CURLOPT_HEADER,         true);
	curl_setopt($my_ch, CURLOPT_NOBODY,         true);
	curl_setopt($my_ch, CURLOPT_RETURNTRANSFER, true);
	curl_setopt($my_ch, CURLOPT_TIMEOUT,        10);
	$r = curl_exec($my_ch);
	 foreach(explode("\n", $r) as $header) {
		if(strpos($header, 'Content-Length:') === 0) {
			return trim(substr($header,16)); 
		}
	 }
	return '';
}

function get_description($url) {
	$fullpage = curlGet($url);
	$dom = new DOMDocument();
	@$dom->loadHTML($fullpage);
	$xpath = new DOMXPath($dom); 
	$tags = $xpath->query('//div[@class="info-description-body"]');
	foreach ($tags as $tag) {
		$my_description .= (trim($tag->nodeValue));
	}	
	
	return utf8_decode($my_description);
}
?>

Download files for fetch the YouTube ID’s and some other curl request in server.

now create download.php file.

<?php

include_once('config.php');
// Check download token
if (empty($_GET['mime']) OR empty($_GET['token']))
{
	exit('Invalid download token 8{');
}

// Set operation params
$mime = filter_var($_GET['mime']);
$ext  = str_replace(array('/', 'x-'), '', strstr($mime, '/'));
$url  = base64_decode(filter_var($_GET['token']));
$name = urldecode($_GET['title']). '.' .$ext; 

// Fetch and serve
if ($url)
{
	$size=get_size($url);
	// Generate the server headers
	if (strpos($_SERVER['HTTP_USER_AGENT'], 'MSIE') !== FALSE)
	{
		header('Content-Type: "' . $mime . '"');
		header('Content-Disposition: attachment; filename="' . $name . '"');
		header('Expires: 0');
		header('Content-Length: '.$size);
		header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
		header("Content-Transfer-Encoding: binary");
		header('Pragma: public');
	}
	else
	{
		header('Content-Type: "' . $mime . '"');
		header('Content-Disposition: attachment; filename="' . $name . '"');
		header("Content-Transfer-Encoding: binary");
		header('Expires: 0');
		header('Content-Length: '.$size);
		header('Pragma: no-cache');
	}

	readfile($url);
	exit;
}

exit('File not found 8{');
?>
Click to download
Click to download

If you have any error of sownload the YouTube video feel free to comment below we have discuss about it.