views:

193

answers:

2

i m using php tube class to download youtube video. Video is automatically downloaded in a folder by submitting youtube video url.

everything is working fine on local (XAMPP), but when i upload this to server(online), THEN ONLY 0 byte flv file is saved.

i also debug the code both side( local and global ).

i get the url like this( on local)

http://www.youtube.com/get_video?video_id=1jNWCFwqPvM&=vjVQa1PpcFMyYCECyfmYENWklpwXGyhVgJpSFPgNAEc%3D

whereas on server i got link like this

http://www.youtube.com/ get_video?video_id=1jNWCFwqPvM&t=vjVQa1PpcFNS3SPxIXi8hy-NF4qZFFrScDXCjGLLrLc%3D

when i copy the link getting from local and paste on browser then a download box appear on browser, whereas when i copy the link getting from server and paste on browser then only a blank page appears

  1. please solve my problem, where is the error???
  2. how to know that pear is installed or not on server?
  3. on server side sometimes i get link like

    http://www.youtube.com/get_video?video_id=1jNWCFwqPvM&t=vjVQa1PpcFNS3SPxIXi8hy-NF4qZFFrScDXCjGLLrLc%3D

but sometimes i get only

http://www.youtube.com/get_video?video_id=&t=

please guide me how to solve these?

UPDATE:

i debug all the code and found that file_get_contents() return string(0) on server whereas on local it return some integer value. allow_url_fopen is onmy server

this is index.php

include_once('functions.php');  
$url =  $_POST['url']; // here is url of youtube video
$pattern = getPatternFromUrl($url); 
$flv_path = GrabFlvFromYoutube($pattern );
echo "File Successfully Downloaded at:".$flv_path."<br/>";

these functions are in functions.php

function getPatternFromUrl($url)
{
$url = $url.'&';
$pattern = '/v=(.+?)&+/';
preg_match($pattern, $url, $matches);
//echo $matches[1]; die;
return ($matches[1]);
}

and

function GrabFlvFromYoutube( $pattern )
{
require_once ("phptube.php");
$tube = new PHPTube ();
$flv_http_path = $tube->download($pattern) ;
    echo "<br/>Complete URL:".$flv_http_path;
set_time_limit(0);

$data = file_get_contents($flv_http_path);  
//var_dump(file_get_contents($flv_http_path));  
$new_flv_path = dirname(_FILE_).'/flvs/'.$pattern.'-'.time().'.flv' ;

echo "<br />File uploaed:";
    file_put_contents($new_flv_path, $data);    

return $new_flv_path ;
}

below function in phptube.php

class PHPTube {

var $cookies;
var $mgr;
var $req; 
var $debug = true;
var $auth = false;

function PHPTube () {}
    function download ($video_id) {

    $url = "http://www.youtube.com/watch?v=".$video_id;     
$this->req =& new HTTP_Request($url);
$response = $this->req->sendRequest();
if (PEAR::isError($response)) {
    $response->getMessage()."\n";
} else {    
        $page = $this->req->getResponseBody();  
    $vpattern = '/v=(.*?)&/';
    //$vpattern ='/video_id:(.*?)/';
    preg_match($vpattern,$page,$mv);
    //preg_match('&"video_id": "(.*?)"&', $page, $mv); 
    echo "<br />Video ID:".$v_id = $mv[1]; 
    //$tpattern='/"t": "(.*?)"/';
    $tpattern = '/&t=(.*?)&/';
    preg_match($tpattern,$page,$tickets);
    echo "<br />Token ID:".$token = $tickets[1];
    $curl = "video_id=";
    $curl .= $v_id;
    $curl .= "&t=";         
    $curl.= $token;
    //echo "<br />Query String:".$curl; die;
    $url = "http://www.youtube.com/get_video?".$curl;  
    if ($this->debug)
     return $url;
     }
   }
 }  

Thanks in advance to all my seniors

A: 

Did you at least try the user agent idea I mentioned in your last question? I'll paste it here again if you didn't.

ini_set('user_agent', 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.9) Gecko/20071025 Firefox/2.0.0.9');

And you're going to be pasting that at the top of your script file. You can also change this in your php.ini file under the user_agent setting.

The only thing I can imagine that would be different between your server and your local machine is that the server is identifying itself differently.

The other thing here is that YouTube could be blocking your server/web host from requesting videos. If you're on a shared host and somebody tried to do this stuff before, Google could have cracked down on it and blacklisted the IP of your host. Downloading FLVs from YouTube is against the terms of service, so they may very well have done this.

Hope this helps (again).

mattbasta
i m using that lines sir, i just paste the code which is necessary to understand my question
diEcho
is there any way to find that youtube has blocked my serever/web host or not??
diEcho
ssh to your server and wget -O- http://youtubevideopageurl check the headers, see if you get a page.I doubt you are blocked, it's because the ips are different between the server that requested the download url and the client that's trying to use that url
jmoz
@jmoz i didn't understand what means of "ssh to your server and wget" please tell me where i have to write lines of code ?
diEcho
Do you have access to your server where you can run commands on the shell? Can you ssh to your server's ip and log in? If so you can run the unix command `wget` which will request a webpage from your server, if one comes back then you're not banned.Alternatively create a php script, which does `file_get_contents()` on a url to a video page, upload to your server and run, if you get the source code then you're not banned.
jmoz
when i m using `var_dump(file_get_contents())` then sometimes i got source code with string("181711") (some value) where sometimes i got string("0")
diEcho
A: 

I think it's something to do with the ip of the machine that attempts to parse their sourcecode or grab a link. On a local machine, since you pull the page, the link will have your ip in the ip param, if it's done on a remote server, that servers ip will be in the ip param and you'll be trying to download from your machine where the ips don't match.

I'm also facing a similiar situation where it's fine on my dev machine but once I put it live it's all gone to pot. I'm trying to figure out how to get it to work, atm the easiest/only thing I can think of is doing on the client with javascript.

EDIT:

My idea of requesting the youtube page using ajax on the client will not work as you can't request from another domain.

The only other way I can think of to get it to work, is let the server get the download link which will be valid for the server's ip, then proxy the file download using readfile() and some header()'s to the client. The big problem with this is that you will absolutely smash your bandwidth streaming downloads to users.

I'd like to know if anyone has any better ideas?

jmoz