@set_time_limit(0);
$id = '8ZF-MIV25Yg';//$_GET['id']; //The youtube video ID
$type = 'video/mp4';//$_GET['type']; //the MIME type of the video
parse_str(file_get_contents('http://www.youtube.com/get_video_info?video_id='.$id),$info);
//var_dump($info); die;
$streams = explode(',',$info['url_encoded_fmt_stream_map']);
foreach($streams as $stream){
parse_str($stream,$real_stream);
$stype = $real_stream['type'];
if(strpos($real_stream['type'],';') !== false){
$tmp = explode(';',$real_stream['type']);
$stype = $tmp[0];
unset($tmp);
}
if($stype == $type && ($real_stream['quality'] == 'large' || $real_stream['quality'] == 'medium' || $real_stream['quality'] == 'small')){
$real_stream['quality'] = 'large';
// var_dump($stype);
header('Content-type: '.$stype);
// header('Transfer-encoding: chunked');
// echo $real_stream['url'].'&signature='.$real_stream['sig']; die;
@readfile($real_stream['url'].'&signature='.$real_stream['sig']); //Change here to do other things such as save the file to the filesystem etc.
ob_flush();
flush();
break;
}
}
die;
Showing posts with label PHP. Show all posts
Showing posts with label PHP. Show all posts
Wednesday, March 26, 2014
Monday, May 21, 2012
How to enable gzip compression with Php.
Enabling Gzip Compression
In many forums and on many webpages it has been widely proclaimed that to enable gzip compression using php is as easy as adding a single line of code at the beginning of each page. This is not false, however it is not the best way of doing it. Before continuing, here's the code. It takes advantage of Php's buffered output and buffers all output of the script until the Php engine has completed, and then runs all the output through a special function that gzip compresses it before sending it on to the browser.
Here is the code. Just place this at the very top of all your Php pages and it will send gzip-compressed output to the browsers.
<?php
ob_start("ob_gzhandler");
?>
This method is dependant on the Apache server and in addition, the mod_gzip module must be installed and loaded.
<?php
ob_start("ob_gzhandler");
?>
This method is dependant on the Apache server and in addition, the mod_gzip module must be installed and loaded.
Other Methods to Enable Gzip Compression
The method mentioned above is quick and easy, but the downfalls are that it only works on Apache with mod_gzip. Not only that, but according to the Php manual, that is not the preferred method for gzipping.
(From the Php manual at www.php.net)Another method of gzipping is as follows:
note that using zlib.output_compression is preferred over ob_gzhandler().
<?php// Include this function on your pages
function print_gzipped_page() {
global $HTTP_ACCEPT_ENCODING;
if( headers_sent() ){
$encoding = false;
}elseif( strpos($HTTP_ACCEPT_ENCODING, 'x-gzip') !== false ){
$encoding = 'x-gzip';
}elseif( strpos($HTTP_ACCEPT_ENCODING,'gzip') !== false ){
$encoding = 'gzip';
}else{
$encoding = false;
}
if( $encoding ){
$contents = ob_get_contents();
ob_end_clean();
header('Content-Encoding: '.$encoding);
print("\x1f\x8b\x08\x00\x00\x00\x00\x00");
$size = strlen($contents);
$contents = gzcompress($contents, 9);
$contents = substr($contents, 0, $size);
print($contents);
exit();
}else{
ob_end_flush();
exit();
}
}// At the beginning of each page call these two functions
ob_start();
ob_implicit_flush(0);
// Then do everything you want to do on the page
echo 'Hello World';
// Call this function to output everything as gzipped content.
print_gzipped_page();
?>
Subscribe to:
Posts (Atom)