Math
Math

Reputation: 676

cURL - File is not downloaded when using curl

For what i'm trying to do, i use PHP5 in CLI, and cURL extension. I'm trying to download a file from youtube's server, it works fine with any navigator, the link is something like that;

`http://youtube.com/get_video_info?video_id=VIDEO_ID

exemple: http://youtube.com/get_video_info?video_id=9pQxmD6Bhd

When i access this file trough my navigator, it prompt me with a download box for the file 'get_video_info', when downloaded the file content some data, ..

The problem is to get this file with cURL, i keep getting this error message;

status=fail&errorcode=2&reason=Invalid+parameters.

This is the code ( i tried to change some option, but i'm not familliar with cURL, so i'm stuck.

$c = curl_init();
curl_setopt($c, CURLOPT_URL, "http://youtube.com/get_video_info?video_id=9pQxmD6Bhd");
curl_setopt($c, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.1 (KHTML, like Gecko) Chrome/21.0.1180.89 Safari/537.1");
curl_setopt($c, CURLOPT_RETURNTRANSFER, true);
curl_setopt($c, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($c, CURLOPT_HEADER, false);
$output = curl_exec($c);
if($output === false)
{
    trigger_error('Erreur curl : '.curl_error($c),E_USER_WARNING);
}
else
{
    var_dump($output);
}
curl_close($c);

I tried to use some curl_setopt options, like CURLOPT_TRANSFERTEXT with no success. I definitely need help !

Thanks for answers, and sorry if i did something that dont respect the rules here, it's my first post.

EDIT Here is the code to download youtube video ( .ogg ) with php in cli.

<?php 

/*Youtube URL and ID*/
$youtube_video = "http://www.youtube.com/watch?v=Ftud51NhY2I";
$yt_id = explode("=", $youtube_video);
$id = $yt_id[1];

/*
    Functions
*/
function get_link($raw){
    $url = rawurldecode(rawurldecode($raw));
    $url = explode("&qual", $url);      
    return $url[0];
}



/*
    Here we go

    Query video token
*/
$c = curl_init();
curl_setopt($c, CURLOPT_URL, $youtube_video);
curl_setopt($c, CURLOPT_RETURNTRANSFER, true);
curl_setopt($c, CURLOPT_HEADER, false);
$output = curl_exec($c);
if($output === false)
{
    trigger_error('Erreur curl : '.curl_error($c),E_USER_WARNING);
}
else{}
curl_close($c);

/*
    Get Video infos
*/
$c = curl_init();
curl_setopt($c, CURLOPT_URL, "http://youtube.com/get_video_info?video_id=".$id);
curl_setopt($c, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.1 (KHTML, like Gecko) Chrome/21.0.1180.89 Safari/537.1");
curl_setopt($c, CURLOPT_RETURNTRANSFER, true);
curl_setopt($c, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($c, CURLOPT_HEADER, false);
$output = curl_exec($c);
if($output === false){trigger_error('Erreur curl : '.curl_error($c),E_USER_WARNING);}
else{}
curl_close($c);
/*Get RAW link*/
$temp = explode("url_encoded_fmt_stream_map=url%3D", $output);
$url = explode("=", $temp[1]);
$url = get_link($url[0]);
/*Get Video name*/
$temp = "";
$temp = explode("title=", $output);
$title = explode("&", $temp[1]);
$title = rawurldecode(rawurldecode($title[0]));
$replace = array(':', '+', '\\', '/', '"', '<', '>', '|', '(', ')', '\'');
$title = str_replace($replace, ' ',$title);
//echo $title;
/*
    Download Video
*/
$url  = $url;
$path = $title.'.ogg';
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$data = curl_exec($ch);
curl_close($ch);
file_put_contents($path, $data);

echo "Done... \r\n";

?>

Upvotes: 0

Views: 2062

Answers (2)

Charith7788
Charith7788

Reputation: 21

youtube has changed their system. now it is working only with the real IP who use the get_video_info system. when you try with cURL it sends the server IP to Youtube, then you have to download videos with the servers IP, because youtube system creates the direct video download urls with given IP.

Upvotes: 0

Mihai Iorga
Mihai Iorga

Reputation: 39704

You get error message because the video_id parameter isn't valid.

Try changing that ID and it should work correctly.

http://www.youtube.com/watch?v=9pQxmD6Bhd - does not exist

Upvotes: 3

Related Questions