YouTube Video Downloader Source Code in PHP
Posted: September 5th, 2009 | Author: admin | Filed under: Php, Web Secrets | 2 Comments »Tags: download youtube video script in php, free youtube video downloader, youtube video download, youtube video downloader source code
Source code for YouTube Video Downloader script has been released. You can use it in your own applications and website without any restriction. The script uses CURL to fetch data from the YouTube server and then display the link to download the video. Furthermore, it uses AJAX so the page will not refresh.
If you find it useful, please do not remove the backlink to this blog, www.web-tricks.info
You can view online demo of the working script:
You can also download the script Free from:
The source code for front-end HTML design is:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>YouTube Easy Video Downloader</title>
<script type="text/javascript" language="javascript" src="js/ajax.lib.js"></script>
<script type="text/javascript" language="javascript" src="js/youtube.download.js"></script>
<style type="text/css">
<!--
#yTubeLinkDiv {
display: none;
color: #00F;
}
#yTubeLinkDiv a{
font-size: 18px;
color: #00C;
text-decoration: underline;
}
#yTubeLinkDiv a:hover{
color: #F00;
}
-->
</style>
</head>
<body>
<p>YouTube Video URL:</p>
<p>
<input name="url" type="text" id="url" size="55" maxlength="1000" />
</p>
<p>
<input name="downloadBtn" type="button" id="downloadBtn" value="Get Download Link" onclick="getLink();" />
</p>
<div id="yTubeLinkDiv"></div>
<p>Powered by <a href="http://www.web-tricks.info" title="Powered by www.Web-Tricks.info">www.Web-Tricks.info</a>
<br />
Please give me credit by placing a backlink to my website, if you find this tool useful</p>
</body>
</html>
There are two javascript files,ajax.lib.js and youtube.download.js. The source code for ajax.lib.js file is:
/*
YouTube Easy Video Downloader Script version 1.0
Created by: Waseem Khan
Dated: September 05, 2009
WebSite: www.Web-Tricks.info
*/
function Ajax(url, method, parameters, responseFunction, returnType)
{
this.url = url;
this.parameters = parameters;
this.method = method.toLowerCase();
this.returnType = returnType.toLowerCase()
this.getXMLHTTPObject = function()
{
if (window.XMLHttpRequest)
{
// check for Safari, Mozilla, Opera...
XMLHttpRequestObject = new XMLHttpRequest();
}
else if (window.ActiveXObject)
{
// check for Internet Explorer
XMLHttpRequestObject = new ActiveXObject("Microsoft.XMLHTTP");
}
if (!XMLHttpRequestObject)
{
alert("Your browser does not support Ajax.");
// return false in case of failure
return false;
}
// return the object in case of success
return XMLHttpRequestObject;
};
// initially set the object to false
var ajaxObject = this.getXMLHTTPObject();
this.sendRequest = function()
{
if(ajaxObject)
{
// continue if the object is idle
if ((ajaxObject).readyState == 4 || (ajaxObject).readyState == 0)
{
// open connection and send request to server
if (this.method == "get")
{
(ajaxObject).open("GET", this.url + "?" + this.parameters, true);
}
else
{
(ajaxObject).open("POST", this.url, true);
}
// send the appropriate headers
(ajaxObject).setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
// set the function to be called on a change in ajaxObj state
(ajaxObject).onreadystatechange = function()
{
if ((ajaxObject).readyState == 4)
{
// continue if the response is healthy
if ((ajaxObject).status == 200)
{
if (this.returnType == "xml")
{
responseFunction(ajaxObject.responseXML);
}
else
{
responseFunction(ajaxObject.responseText);
}
}
}
};
// set additional parameters (to be sent to server) to null
if (this.method == "get")
{
(ajaxObject).send(null);
}
else
{
(ajaxObject).send(this.parameters);
}
}
}
};
this.sendRequest();
}
The code for youtube.download.js is:
/*
YouTube Easy Video Downloader Script version 1.0
Created by: Waseem Khan
Dated: September 05, 2009
WebSite: www.Web-Tricks.info
*/
function trim(s)
{
return s.replace(/(^\s+)|(\s+$)/g, "");
}
function getLink()
{
var yURL = document.getElementById('url').value;
if (trim(yURL) == "")
{
alert("You must provide a valid YouTube Video URL");
return;
}
var url = "youtube.download.php";
var params = "url=" + encodeURIComponent(yURL);
var ajax = new Ajax(url, "POST", params, yResponse, "Text");
var yUrlDiv = document.getElementById('yTubeLinkDiv');
yUrlDiv.style.display = "block";
yUrlDiv.innerHTML = "Fetching download link..."
}
function yResponse(res)
{
var yUrlDiv = document.getElementById('yTubeLinkDiv');
if (trim(res) == "")
{
yUrlDiv.style.display = "none";
alert("The YouTube Video URL you provided is invalid or the video has been removed.");
}
else
{
var url = 'Download Video in FLV format';
yUrlDiv.style.display = "block";
yUrlDiv.innerHTML = url;
}
}
The file which is called by the AJAX is youtube.download.php, source code is below:
<?php
/*
YouTube Easy Video Downloader Script version 1.0
Created by: Waseem Khan
Dated: September 05, 2009
WebSite: www.Web-Tricks.info
*/
if(!isset($_POST['url']) || trim($_POST['url']) == "")
{
echo "<h1>Invalid YouTube URL</h1>"; exit;
}
$target = strip_tags($_POST['url']);
define("WEBBOT_NAME", "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.0.9) Gecko/2009040821 Firefox/3.0.9");
define("CURL_TIMEOUT", 25);
define("COOKIE_FILE", "cookie.txt");
function openYoutubeLink($target, $ref)
{
$ch = curl_init();
curl_setopt($ch, CURLOPT_HEADER, EXCL_HEAD);
curl_setopt($ch, CURLOPT_NOBODY, FALSE);
curl_setopt ($ch, CURLOPT_HTTPGET, TRUE);
curl_setopt ($ch, CURLOPT_POST, FALSE);
curl_setopt($ch, CURLOPT_COOKIEJAR, COOKIE_FILE);
curl_setopt($ch, CURLOPT_COOKIEFILE, COOKIE_FILE);
curl_setopt($ch, CURLOPT_TIMEOUT, CURL_TIMEOUT);
curl_setopt($ch, CURLOPT_USERAGENT, WEBBOT_NAME);
curl_setopt($ch, CURLOPT_URL, $target);
curl_setopt($ch, CURLOPT_REFERER, $ref);
curl_setopt($ch, CURLOPT_VERBOSE, FALSE);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
curl_setopt($ch, CURLOPT_MAXREDIRS, 4);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
$res = curl_exec($ch);
curl_close($ch);
return $res;
}
function splitString($string, $delineator, $desired, $type)
{
$lc_str = strtolower($string);
$marker = strtolower($delineator);
if($desired == BEFORE)
{
if($type == EXCL)
$split_here = strpos($lc_str, $marker);
else
$split_here = strpos($lc_str, $marker)+strlen($marker);
$parsed_string = substr($string, 0, $split_here);
}
else
{
if($type==EXCL)
$split_here = strpos($lc_str, $marker) + strlen($marker);
else
$split_here = strpos($lc_str, $marker) ;
$parsed_string = substr($string, $split_here, strlen($string));
}
return $parsed_string;
}
function returnBetween($string, $start, $stop, $type)
{
$temp = splitString($string, $start, AFTER, $type);
return splitString($temp, $stop, BEFORE, $type);
}
$ref = "http://www.youtube.com/";
$res = openYoutubeLink($target, $ref);
$start = 'var swfArgs = {"';
$stop = '};';
$res = returnBetween($res, $start, $stop, EXCL);
$res = urldecode($res);
$res = returnBetween($res, '5|http://', '",', EXCL);
echo trim($res);
?>

