Archive for the ‘PHP’ Category

Download Youtube with PHP and CURL

Tuesday, April 15th, 2008

This is an update for my previous Youtube Downloader.

If you don’t know what is PHP Youtube Downloader, it is a simple PHP script that we can use to download video from Youtube.com. You must, at least, having PHP installed and CURL activated.

Download the source at here and read some information at Google Code Page.

Have fun!

Youtube downloader is working!

Monday, March 3rd, 2008

Mobi.web.idFor more than a month my youtube downloader script was not working due to youtube changed their layout. Today I’ve rewritten the script and simplified the functionality. Now mobi.web.id is running again and ready to help everyone download videos from Youtube.

Previous script consist of four functions and I’ve shortened into two functions. Please hit the read button to see the full working script.

Here’s the shorten code for youtube downloader: function get_http_header($url){
$uh = curl_init();
curl_setopt($uh, CURLOPT_URL, $url);
curl_setopt($uh, CURLOPT_HEADER, 1);
curl_setopt($uh, CURLOPT_RETURNTRANSFER, 1);
$res = curl_exec($uh);
curl_close($uh);
return $res;
}

function show_url($http_header){
$arai = explode(”\n”,$http_header);
foreach($arai as $ini){
if(eregi(”location”,$ini)) $url = $ini;
}
list($sampah,$hasil) = explode(”Location:”,$url);
return str_replace(”\n”,”",trim($hasil));
}

function download_youtube($url){
if(ereg(’&',$url)){
if (eregi(”watch\?v=(.*)&”, $url, $out)) {
$video_id = $out[1];
}
}
else{
list($none,$video_id) = explode(’watch?v=’,$url);
}
$secret_link = substr(show_url(get_http_header(’http://youtube.com/v/’.trim($video_id))),11);
$video_url = show_url(get_http_header(’http://youtube.com/get_video?’.$secret_link));
list($dl_url,$sign) = explode(”&signature”,$video_url);
return $dl_url;
}

// EXAMPLE:
echo download_youtube(’http://www.youtube.com/watch?v=5uy7SAidyTM’);
?>

Have phun, may the source be with you! ^_^

PHP code highlight

Wednesday, February 27th, 2008

As you may see in this blog, all PHP codes will be highlighted so they become readable and looking good. This time I will share my script to highlight the code inside a bunch of text.

Yes, it will highlight string inside [code] and [/code] only, like following sample:

Here is a sample of PHP script:
[code]
<?php
if($name == ‘neo’){
    echo ‘Hei you!’;
}
?>
[/code]
then save it as sample.php.

The result will be displayed as follow:

Here is a sample of PHP script:

if($name == 'neo'){
echo 'Hei you!';
}
?>

then save it as sample.php.

Now we see the magic behind the highlighting process:
// function to call
function highlight_code($txt){
$hasil = preg_replace_callback('{\[code\]((.|\n)+?)\[/code\]}i',"replace_code", $txt);
return $hasil;
}

// main function
function replace_code($ketemu){
$hasil = trim($ketemu[1], "\n ");
return highlight_string($hasil, true);
}

// sample of usage:
$string = "It's PHP info: [ code ] [ /code ]“;
echo highlight_code($string);
?>

The main function is the replace_code() function that will highlight the string. But we need to return the the highlighted string back into the full strings. So we use the callback, using highlight_code() function.

Good luck ^_^

Get DPI value of an image using PHP

Tuesday, February 19th, 2008

This is a simple function I wrote to get a DPI (dot per inch) value from an image using PHP only, without the need of ImageMagick nor GD library. The script was inspired from denisb post (the third post). At the forum, he described how to get the DPI value stored within the file.

function get_dpi($filename){

// open the file and read first 20 bytes.
$a = fopen($filename,'r');
$string = fread($a,20);
fclose($a);

// get the value of byte 14th up to 18th
$data = bin2hex(substr($string,14,4));
$x = substr($data,0,4);
$y = substr($data,4,4);
return array(hexdec($x),hexdec($y));

}

// output the result:
print_r(get_dpi2('filename.jpg'));
?>

I have tried this function to get DPI value of an image that I generated using Photoshop and worked as expected but failed to get DPI value from an image from any digital camera. It returned weird value instead.

Any idea why this function failed to retrieve DPI value from image that generated by digital camera? Please share your opinion.

Youtube Downloader using PHP

Monday, January 7th, 2008

This short tutorial will show you how to download videos from Youtube using PHP. CURL is needed for this script because many webhosting now prohibit remote_fopen.

First function, get HTML content from an URL: function get_content_of_url($url){
$ohyeah = curl_init();
curl_setopt($ohyeah, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ohyeah, CURLOPT_URL, $url);
$data = curl_exec($ohyeah);
curl_close($ohyeah);
return $data;
}
?>

Second function, get string that contain the clue of where the video file is located: function get_flv_link($string) {
if (eregi("watch_fullscreen\?video_id=(.*)&title=", $string, $out)) {
$outdata = $out[1];
}
return 'http://youtube.com/get_video.php?video_id='.$outdata;
}
?>

Next function, “visit” the link that we got from second function above. We will read the header and find out the real file location: function get_http_header($url){
$uh = curl_init();
curl_setopt($uh, CURLOPT_URL, $url);
curl_setopt($uh, CURLOPT_HEADER, 1);
curl_setopt($uh, CURLOPT_RETURNTRANSFER, 1);
$res = curl_exec($uh);
curl_close($uh);
return $res;
}
?>

Function above will return a bunch of HTTP headers and we don’t need them all. This function will parse the HTTP headers and only return the video location: function show_url($http_header){
$arai = explode("\n",$http_header);
foreach($arai as $ini){
if(eregi("location",$ini)) $url = $ini;
}
list($sampah,$hasil) = explode("Location:",$url);
return str_replace("\n","",trim($hasil));
}
?>

Last thing to do, is bundle all functions above: function download_youtube($url){
$data = get_content_of_url($url);
$next_url = get_flv_link($data);
$data = get_http_header($next_url);
return show_url($data);
}
?>

Wait, how to use the youtube downloader? It’s very simple: $url = "http://youtube.com/watch?v=SAQZ0BDXn48";
echo download_youtube($url);
?>

That’s all folks. It’s a very interesting and chalenging moment when I wrote any kind of grabbing scripts. Hope it helps yo all.

Veoh’s Video Downloader

Sunday, January 6th, 2008

Here is the script that you can use to download videos from veoh.com. I wrote this script to bid a project at rentacoder.com and finished the code within half an hour. Consist of three functions and you can put them all into a class. But I prefer let them apart.

// this is used to get url content
function get_url_content($url){
$ch = curl_init();
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_URL, $url);
$data = curl_exec($ch);
curl_close($ch);
return $data;
}// this function used to retrieve value inbetween specific delimiter
function get_inbetween($tag1,$tag2,$string) {
if (eregi(”$tag1(.*)$tag2″, $string, $out)) {
$outdata = $out[1];
}
return $outdata;
}

// this function used to download music from veoh.com
// URL: http://www.veoh.com/videos/v1734061jbnYFjPj
function get_veoh($url){

// get the music_id
$ari = explode(”/”,$url);
$v_id = array_pop($ari);

// retrieve xml files
$data = get_url_content(”http://www.veoh.com/rest/video/”.$v_id.”/details”);

// retrieve path into music files
$hasil = get_inbetween(”fullPreviewHashPath”,”fullPreviewToken=”,$data);
$hasil = str_replace(array(’”‘,’='),”",$hasil);

// yell it loud
return trim($hasil);
}

echo get_veoh(”http://www.veoh.com/videos/v1734061jbnYFjPj”);
?>

Podcast - Listen to the blog posts

Friday, December 7th, 2007

This player will play audio for every blog post available in this blog. You will see entries as a playlist and just click to hear the voice. Now you can listen to this blog, not only read ^_^

Powered by TalkR.com

Check Filesize Of Remote File Using PHP

Wednesday, November 28th, 2007

This script is used to check filesize for a file located on a remote (far away) web server.

function get_remote_size($url){
$uh = curl_init();
curl_setopt($uh, CURLOPT_URL, $url);

// set NO-BODY to not receive body part
curl_setopt($uh, CURLOPT_NOBODY, 1);

// set HEADER to be false, we don't need header
curl_setopt($uh, CURLOPT_HEADER, 0);

// retrieve last modification time
curl_setopt($uh, CURLOPT_FILETIME, 1);
curl_exec($uh);

// assign filesize into $filesize variable
$filesize = curl_getinfo($uh,CURLINFO_CONTENT_LENGTH_DOWNLOAD);

// assign file modification time into $filetime variable
$filetime = curl_getinfo($uh,CURLINFO_FILETIME);
curl_close($uh);

// push out
return array("size"=>$filesize,”time”=>$filetime);
}
// You can use it as follow:
print_r(get_remote_size(”http://downloads.videolan.org/pub/videolan/vlc/0.8.6c/win32/vlc-0.8.6c-win32.exe”));
?>

Hope it’s cool ^_^

Force Download Image File

Wednesday, November 28th, 2007

This script is useful when we want to download an image file (JPG, PNG, GIF) instead of preview it into our browser. A download dialog will appear and the image will not be displayed.

// Define the image type. We can remove this but not recommended
header("Content-type: image/jpeg");

// Define the name of image after downloaded
header('Content-Disposition: attachment; filename="file.jpg"');

// Read the original image file
readfile('file.jpg');
?>

Let’s give it a try. Save the script as download.php and put file file.jpg in the same directory/folder as download.php. Then open download.php using our web browser.

We should now see download dialog instead of file.jpg opened in the browser.

Old PHP script in register global = off environment

Wednesday, September 5th, 2007

My friend asked me how to run PHP scripts (which is written for register global = on environment) in a register global = off environment. Yeah, it’s a bad thing to write PHP script for register global = on environment, he just wanted his old scripts work with minimum efforts.

So I gave him a short script, which will regenerate variables from $_POST or $_GET. This script need to be placed onto the top of every single file which required variable from POST or GET method.

foreach($_POST as $key=>$value){
$$key = $value;
}
foreach($_GET as $key=>$value){
$$key = $value;
}
?>

Above script will, for example, generate variable named $username from $_POST['username'] with the same value.

Just remember to put above script in the top of your scripts, and remember NOT to write bad scripts.