Jump to content

YouTube video upload class


EMG

Recommended Posts

Hi guys,

I spend the whole morning on this class, but i can't find the problem.
The idea is that you can upload video's to youtube using this class.

What works:
[list]
[*]The login part (signing in and get redirected to /index
[*]The add part (completing in the form and get redirected to step 2)
[/list]

What doesn't want to work:
[list]
[*]Posting the video to the youtube servers[quote]I search the post url out of the form in step 2 (i got it somewhere out of a javascript code).
I'm working with the flash posting url because the normal posting url gives a 404 error
(and actually the flash one is giving a 400, which is better i guess)[/quote]
[/list]

Here is the class i use:
[code]<?php
/********************************************************/
/* */
/* Copyright 2006 Exclamation Media Group */
/* */
/* All classes are written by Andreas Creten */
/* www.andreascreten.be */
/* */
/* */
/********************************************************/

// ------------------- CLASS YOUTUBE ------------------- //
// with this class you can upload a video to youtube //
// //
// how does it work:   //
// - initialize the class: //
// $up = new youtube(); //
// - login with your own username and password: //
// $up->login('username', 'password'); //
// - make a new movie handler: //
// $up->add('title', 'description', 'keyw');//
// - upload the movie to the handle: //
// $up->upload('themovieurl'); //
// //
// ----------------------------------------------------- //
// Copyright 2006 Andreas Creten //
// ----------------------------------------------------- //

class youtube {
var $connection;
// set urls for youtube
var $login_url = "http://www.youtube.com/signup";
var $upload_form_url = "http://www.youtube.com/my_videos_upload";
var $upload_post_url;

var $cookie_location = 'cookie.txt';

var $post_vars = array();

function __construct() {
// init curl session
$this->connection = curl_init();

//return header
curl_setopt($this->connection, CURLOPT_HEADER, 1);

//return data as string
curl_setopt($this->connection, CURLOPT_RETURNTRANSFER,1);

// follow redirects
curl_setopt($this->connection, CURLOPT_FOLLOWLOCATION,1);
// connection timeout
curl_setopt($this->connection, CURLOPT_CONNECTTIMEOUT,10);

// set cookies
curl_setopt($this->connection, CURLOPT_COOKIESESSION , 1);
curl_setopt($this->connection, CURLOPT_COOKIEJAR, $this->cookie_location);
}

function login($username, $password) {
// set dynamic form vars
$this->postvarAdd('username',$username);
$this->postvarAdd('password',$password);

// set static form vars
$this->postvarAdd('action_login', "Log In");
$this->postvarAdd('current_form', "loginForm");

// set curl options
curl_setopt($this->connection, CURLOPT_URL, $this->login_url);

// set curl post vars
curl_setopt($this->connection, CURLOPT_POST, 1);
curl_setopt($this->connection, CURLOPT_POSTFIELDS, $this->postvarString());

// delete form vars which are not needed anymore
$this->postvarDel('username', 'password', 'action_login', 'current_form');

// execute curl
return curl_exec($this->connection);
}

function add($title, $description = null, $keywords = null, $categories = 1, $privacy = "public", $language = "EN") {
// set dynamic form vars
$this->postvarAdd('field_myvideo_title', $title);
$this->postvarAdd('field_myvideo_descr', $description);
$this->postvarAdd('field_myvideo_keywords', $keywords);
$this->postvarAdd('field_myvideo_categories', $categories);
$this->postvarAdd('language', $language);
$this->postvarAdd('field_privacy', $privacy);

// set static form vars
$this->postvarAdd('field_command', 'myvideo_submit');
$this->postvarAdd('action_upload', 'Continue -&gt;');

// set curl options
curl_setopt($this->connection, CURLOPT_REFERER, $this->login_url);
curl_setopt($this->connection, CURLOPT_URL, $this->upload_form_url);

// set curl post vars
curl_setopt($this->connection, CURLOPT_POST, 1);
curl_setopt($this->connection, CURLOPT_POSTFIELDS, $this->postvarString());

// delete form vars which are not needed anymore
$this->postvarDel('action_upload');

// execute curl
$output = curl_exec($this->connection);

// get the post url out of the output of curl
$output_t = explode("var upload_url = \"", $output);
$output_t = explode("\";", $output_t[1]);

// write the form url
$this->upload_post_url = $output_t[0];

return $output;
}

function upload($url) {
if($this->upload_post_url != null) {
// set dynamic form vars
$this->postvarAdd('field_uploadfile', $url);

// set static form vars
$this->postvarAdd('&action_upload', 'Upload Video');
$this->postvarAdd('MAX_FILE_SIZE', '104857600');

// set curl options
curl_setopt($this->connection, CURLOPT_REFERER, $this->upload_form_url);
curl_setopt($this->connection, CURLOPT_URL, $this->upload_post_url);

// set curl post vars
curl_setopt($this->connection, CURLOPT_POST, 1);
curl_setopt($this->connection, CURLOPT_POSTFIELDS, $this->postvarString());

// return an array with the curl output and the headers [return, info]
return array("return" => curl_exec($this->connection), "info" => curl_getinfo($this->connection));
}
else die("You need to make a new movie first!");
}

// this function adds vars to the post array
function postvarAdd($name, $value) {
$this->post_vars[$name] = $value;
}

// this function deletes the asked vars in the post array
function postvarDel() {
foreach(func_get_args() as $argument) {
unset($this->post_vars[$argument]);
}
}

// this function converts the post array to an url string
function postvarString() {
$output = "";
foreach($this->post_vars as $key => $value) {
$i++;
if($i>1) $output .= "&";
$output .= "$key=$value";
}
return $output;
}

// close the curl session
function __destruct() {
curl_close($this->connection);
}
}

?>
[/code]

And this is the code to execute the class (and format the output):
[code]
<?php
$yt = new youtube();
$yt->login("username","password");
$yt->add("domino","A couple of domino blocks","domino, blocks");

echo "<pre>\n";
print_r($yt->upload('http://galleryish.com/test/youtube/domino.mov'));
echo "\n</pre>\n";
?>[/code]

Thanks!

Andreas Creten
Link to comment
https://forums.phpfreaks.com/topic/28226-youtube-video-upload-class/
Share on other sites

[quote author=thorpe link=topic=116031.msg472586#msg472586 date=1164288567]
You might want to narrow down your problem and come back when you have a question formed.
[/quote]

I think my question is quite clear, can someone help me to find out why i got an 400 error when i post a video to youtube via an interface that's exact copy of the youtube interface.

(with interface i mean the class)
[quote author=Jenk link=topic=116031.msg472594#msg472594 date=1164289455]
You'll need to bughunt yourself first. There's no way I'm going to do it all for you. Run some tests, find out which segment of your code is causing the problem. FYI: by segment, I mean line.
[/quote]

I did that, it's working fine.
But the interaction with youtube causes problems.
There is no real error in the PHP!

And i just can't find out what i'm doing wrong ...

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.