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: June 8, 2024, 8:22 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



Messages In This Thread
PHP Locally - by Darwinian - June 7, 2012 at 3:38 pm
RE: PHP Locally - by Jackalope - June 7, 2012 at 3:54 pm
RE: PHP Locally - by Darwinian - June 7, 2012 at 3:57 pm
RE: PHP Locally - by Jackalope - June 7, 2012 at 4:03 pm
RE: PHP Locally - by Darwinian - June 7, 2012 at 4:07 pm
RE: PHP Locally - by Jackalope - June 7, 2012 at 4:20 pm
RE: PHP Locally - by Darwinian - June 7, 2012 at 4:28 pm
RE: PHP Locally - by Tiberius - June 7, 2012 at 4:22 pm
RE: PHP Locally - by Jackalope - June 7, 2012 at 4:26 pm
RE: PHP Locally - by Tiberius - June 7, 2012 at 4:27 pm
RE: PHP Locally - by Jackalope - June 7, 2012 at 4:31 pm
RE: PHP Locally - by Darwinian - June 7, 2012 at 4:32 pm
RE: PHP Locally - by Tiberius - June 7, 2012 at 4:43 pm
RE: PHP Locally - by Darwinian - June 7, 2012 at 4:44 pm
RE: PHP Locally - by Tiberius - June 7, 2012 at 5:01 pm
RE: PHP Locally - by Autumnlicious - June 7, 2012 at 5:29 pm
RE: PHP Locally - by Tiberius - June 7, 2012 at 6:17 pm
RE: PHP Locally - by Autumnlicious - June 7, 2012 at 6:44 pm
RE: PHP Locally - by Jackalope - June 7, 2012 at 7:05 pm
RE: PHP Locally - by Autumnlicious - June 7, 2012 at 8:05 pm
RE: PHP Locally - by Jackalope - June 7, 2012 at 8:15 pm
RE: PHP Locally - by Autumnlicious - June 7, 2012 at 8:31 pm
RE: PHP Locally - by Jackalope - June 7, 2012 at 8:39 pm
RE: PHP Locally - by Autumnlicious - June 7, 2012 at 8:56 pm
RE: PHP Locally - by Jackalope - June 7, 2012 at 9:04 pm
RE: PHP Locally - by Autumnlicious - June 8, 2012 at 1:33 am
RE: PHP Locally - by Jackalope - June 8, 2012 at 1:47 am
RE: PHP Locally - by Darwinian - June 8, 2012 at 2:24 am
RE: PHP Locally - by Autumnlicious - June 8, 2012 at 12:44 pm
RE: PHP Locally - by Colanth - June 15, 2012 at 10:03 pm
RE: PHP Locally - by Jackalope - June 15, 2012 at 11:42 pm
RE: PHP Locally - by Darwinian - June 16, 2012 at 7:30 am
RE: PHP Locally - by Tiberius - June 16, 2012 at 7:37 am
RE: PHP Locally - by Darwinian - June 16, 2012 at 7:41 am
RE: PHP Locally - by Tiberius - June 16, 2012 at 11:09 am



Users browsing this thread: 1 Guest(s)