Our server costs ~$56 per month to run. Please consider donating or becoming a Patron to help keep the site running. Help us gain new members by following us on Twitter and liking our page on Facebook!
Current time: November 23, 2024, 12:27 am

Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
PHP Locally
#1
PHP Locally
OK, I'd like to be able to get the following code to run on my computer rather than my server.

It's code to download a youtube clip and save it as an flv file and, on my server, it works brilliantly. However, I'd like to somehow include it as a feature for my media player so that users can do the same but on their own P.C.

Any ideas?

Code:
<?php

class youtubegrabber{

    var $youtube_video_url;
    var $test;
    var $final_flv_filename;
    var $cookies_path;
    var $curl_headers;
    var $flv_url;

    function __construct($youtube_video_url, $final_flv_filename, $test = 0){
        $this->youtube_video_url = $youtube_video_url;
        $this->test = $test;
        $this->final_flv_filename = $final_flv_filename;
        
        $this->youtube_video_id = $this->get_youtube_video_id();
        
        $this->cookies_path = "cookies.txt";
        $clear_cookies = $this->clear_cookies();
        
        $this->curl_headers = array(
            "Accept-Language: en-us",
            "User-Agent: Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv:1.9.2.15) Gecko/20110303 Firefox/3.6.15",
            "Connection: Keep-Alive",
            "Cache-Control: no-cache"
            );
            
        $this->flv_url = $this->get_flv_url();
        
        $save_binary = $this->get_curl_binary();
        $clear_cookies = $this->clear_cookies();
    
    }
    
    function get_youtube_video_id(){
        $thearray = explode("watch?v=", $this->youtube_video_url);
        return $thearray[1];
    }
    
    
    function clear_cookies(){
    
        if(file_exists($this->cookies_path)){
            unlink($this->cookies_path);
        }
        
        $ourFileName = $this->cookies_path;
        $ourFileHandle = fopen($ourFileName, 'w') or die("can't open file");
        fclose($ourFileHandle);
        
    }
    
    function get_flv_url(){
    
        $html = $this->curl_get_url($this->youtube_video_url);
        
      
        preg_match_all("/var.*?swf.*?=.*?\"(.*?)watch-player.*?innerHTML.*?=.*?swf/is", $html, $matches);
        
        
        $decoded = urldecode($matches[1][0]);
        preg_match_all("/url=(.*?)\,/is", $decoded, $matches);
        $matches = $matches[1];
        
        
        for($i = 0; $i < count($matches); $i++){
            $test = explode("&", $matches[$i]);
            $matches[$i] = $test[0];
            $matches[$i] = urldecode($matches[$i]);
        }
        
      
        $final_flv_url = "";
        
        foreach($matches AS $this_url){
            $headers = $this->curl_get_headers($this_url);
            
            $headers = split("\n", trim($headers));
            foreach($headers as $line) {
                if (strtok($line, ':') == 'Content-Type') {
                    $parts = explode(":", $line);
                    $content_type = strtolower(trim($parts[1]));
                    if ( $this->contains("video/x-flv", $content_type) ){
                        $final_flv_url = $this_url;
                        return $final_flv_url;
                    }
                }
            }
        }
        
        return false;
        
    }
    
    function curl_get_url($url){
        $cookie_path = $this->cookies_path;
        
        $headers = $this->curl_headers;
            
        
        $ch = curl_init();    
        //$referer = 'http://www.google.com/search';
        curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); // this pretends this scraper to be browser client IE6 on Windows XP, of course you can pretend to be other browsers just you have to know the correct headers
        //curl_setopt($get, CURLOPT_REFERER, $referer); // lie to the server that we are some visitor who arrived here through google search
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        // set user agent
        //curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv:1.9.2.13) Gecko/20101203 Firefox/3.6.13");
        curl_setopt($ch, CURLOPT_BINARYTRANSFER, 1);
        curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie_path);
        curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie_path);
        curl_setopt( $ch, CURLOPT_FOLLOWLOCATION, true );
        curl_setopt( $ch, CURLOPT_CONNECTTIMEOUT, 5 );
        
        $output = curl_exec($ch);
        $info = curl_getinfo($ch);
        curl_close($ch);
        
        return $output;
    }
    
    function curl_get_headers($url){
        $cookie_path = $this->cookies_path;
        
        $headers = $this->curl_headers;
            
        $ch = curl_init();    
        //$referer = 'http://www.google.com/search';
        curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_HEADER, 1);
        curl_setopt($ch, CURLOPT_NOBODY, 1);
        curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie_path);
        curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie_path);
        curl_setopt( $ch, CURLOPT_FOLLOWLOCATION, true );
        curl_setopt( $ch, CURLOPT_CONNECTTIMEOUT, 5 );
        
        $results = curl_exec($ch);
        
        return $results;
    }
    
    function get_curl_binary(){
        $url = $this->flv_url;
        $cookie_path = $this->cookies_path;
        $headers = $this->curl_headers;
          $final_flv_filename = $this->final_flv_filename;
          
          $ch = curl_init($url);
          $fp = fopen($final_flv_filename, "w");
          curl_setopt($ch, CURLOPT_FILE, $fp);
          curl_setopt($ch, CURLOPT_BINARYTRANSFER, 1);
          curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true );
        curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie_path);
        curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie_path);
          curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); // this pretends this scraper to be browser client IE6 on Windows XP, of course you can pretend to be other browsers just you have to know the correct headers
          curl_exec($ch);
          curl_close($ch);
          fclose($fp);
    }
    
    function contains($substring, $string) {
            $pos = strpos($string, $substring);
    
            if($pos === false) {
                    // string needle NOT found in haystack
                    return false;
            }
            else {
                    // string needle found in haystack
                    return true;
            }
    
    }



}

//$url = "http://www.youtube.com/watch?v=XZxo7IznQnk";
//$filename = "test.flv";
//$youtubegrabber = new youtubegrabber($url, $filename, 0);
//echo "Should be done.<br>";
?>
[Image: cinjin_banner_border.jpg]
Reply
#2
RE: PHP Locally
What is it you're trying to do, execute the PHP locally from your app, or convert the code to Delphi to run inside your app?
Reply
#3
RE: PHP Locally
(June 7, 2012 at 3:54 pm)Cthulhu Dreaming Wrote: What is it you're trying to do, execute the PHP locally from your app, or convert the code to Delphi to run inside your app?

Ideally convert it to Delphi but anyway to get it to work locally is ok with me.
[Image: cinjin_banner_border.jpg]
Reply
#4
RE: PHP Locally
(June 7, 2012 at 3:57 pm)Darwinian Wrote:
(June 7, 2012 at 3:54 pm)Cthulhu Dreaming Wrote: What is it you're trying to do, execute the PHP locally from your app, or convert the code to Delphi to run inside your app?

Ideally convert it to Delphi but anyway to get it to work locally is ok with me.

Well, I would think that you'd be able to install a PHP interpreter locally to get it to work, though I agree with you that getting it to work natively in your app is better.

Unfortunately, I know fuck all about Delphi's APIs - but the PHP code itself is fairly trivial (though is somewhat obtuse in parts). All it's doing is retrieving a given URL, extracting the URL of the FLV video stream, and then downloading that to a file. Assuming Delphi has reasonable APIs for retrieving data by URL, HTTP/XML manipulation and string matching, it shouldn't be too difficult to replicate.
Reply
#5
RE: PHP Locally
I'll have to study it a bit then :S
[Image: cinjin_banner_border.jpg]
Reply
#6
RE: PHP Locally
Hey, all you gotta do is port your app to C or Java and I can be more help. Big Grin
Reply
#7
RE: PHP Locally
Easiest way to run PHP (on Windows) is to install an apache server and PHP. You can do this easily using XAMPP: http://www.apachefriends.org/en/xampp-windows.html

Then you should just be able to create the PHP file in the apache directory and access it through the browser using http://localhost/path/to/file.php

Edit: Looking at the code, it seems to me it just fetches the HTML and extracts the .flv URL from that, then saves that as the file. Should be easy to do with any HTML library.
Reply
#8
RE: PHP Locally
You don't even need Apache. You can execute the script directly by calling the PHP interpreter directly.
Reply
#9
RE: PHP Locally
(June 7, 2012 at 4:26 pm)Cthulhu Dreaming Wrote: You don't even need Apache. You can execute the script directly by calling the PHP interpreter directly.
Yeah, my bad. I thought he just wanted to run the file, and the easiest (i.e. most non-technical) way to do that is via Apache since it handles everything for you.
Reply
#10
RE: PHP Locally
(June 7, 2012 at 4:20 pm)Cthulhu Dreaming Wrote: Hey, all you gotta do is port your app to C or Java and I can be more help. Big Grin

You could always translate it to C and then compile it into a command line executable that I could run like this..

youtubedownload.exe "http://www.youtube.com/watch?v=pTtsn2Srm3E" "C:\test.flv"

Tiger

If you're not too busy Big Grin

I'd give you a write-up in the credits Great
[Image: cinjin_banner_border.jpg]
Reply





Users browsing this thread: 2 Guest(s)