At the request of our visitors are presenting PHP Youtube Downloader 2.0 which is the new version of the PHP script that allows you to download videos from YouTube using PHP. This new version is adapted to the new data structure of Youtube.
Note: This information is for educational purposes, the author is not responsible for the uses that can make users of this information
How does it work?
For a detailed explanation of how the paths of Youtube can read: Constructing YouTube FLV URL on client-side without any server-side script. But summing up when you want to access a video from Youtube, the first generating a session identifier that is returned to the user and then makes the call to the video with the identifier of the video and the session identifier of the form:
HTML Code:
http://www.youtube.com/get_video.php?video_id=idvideo&t=idsesion
So to get the videos, our goal is to get the idsesion to build the final route of the download.
Implementing
The first thing we do is build a function that connects to Youtube and get the headers of the initial response to the request of the video, among these headers will be the identifier we need. Note that this function receives the identifier as a parameter of the video.
PHP Code:
function getHeaders($varurl) { $host = "www.youtube.com"; $url = "/v/".$varurl; $session = ""; $fp = fsockopen ($host, 80, $errno, $errstr, 45); if ($fp) { fputs ($fp, "GET $url HTTP/1.0\r\n\r\n"); while (!feof($fp)) { $char = fgetc($fp); if($char === "\n") { if (ord($header) === 13) { return($session); } else { $arrValue = split(": ", trim($header)); if ($arrValue[0] == "Location") { parse_str($arrValue[1], $getvars); $session = $getvars['t']; } } unset($header); } else { $header = $header.$char; } } fclose ($fp); }}
El siguiente paso es crear la URL con la ruta final del vídeo que deseamos acceder, en nuestro caso estamos recibiendo el identificador del vídeo por método GET de un formulario y estamos construyendo el URL mostrado al inicio en donde obtenemos el identificador de sesión haciendo uso de la función getHeaders.
PHP: if ($_GET['v'] != "") { $videoIds = $_GET['v']; $videoUrl = "http://www.youtube.com/get_video.php"; header("Location: ".$videoUrl."?video_id=".$videoIds."&t=".getHeaders($videoIds)); exit();}
Uniting the code, adding a form where the user enters the id of the movie we get the new version of our tool to download videos from YouTube just using PHP. You can see the example in running:
http://ejemplos.unijimpe.net/phpyoutube.php
Finally they leave the source files of this example so that it can revise and improve.
Download: phpyoutube2.0.zip - File Shared from Box.net - Free Online File Storage Source: Unjimpe.net Traduction: By my