PHPTube - YouTube API for Video Upload & Download
YouTube’s API - basically a read-only set of RSS-feeds and XML/RPC-calls - currently doesn’t support scripted upload or download of videofiles. My quick hack PHPTube offers this featureset in a cleanly encapsulated PHP-class. Special interest goes to the upload-functionality, which virtually allows the integration of YouTube as a video-storage-backend into any other web-app. And while there are several download-scripts out there on the web, I’ve yet to find another uploader.
I’ve originally built PHPTube for using it within PLAY.FM (talking about automated upload of video-footage from our live radio-broadcasts), but since these things take some time I’ld love to hear if anyone out there has some use for my script…a simple digg would do as well
Warning: due to changes in the YouTube’s HTML, all versions prior 0.1.3 are obsolete!
Download: PHPTube 0.1.4 (added support for PHP 4)
Requirements: PHP, PEAR/HTTP
Attention: If you’re looking for PEAR, goto http://pear.php.net/ and read http://pear.php.net/manual/en/installation.getting.php!
Ports: Java-port of PHPTube (by James Schopp), Python-Port (by Sylvain Boily), Joomla-Plugin (download-only, by Mohamad Ballouk) - thx!
Using PHPTube? Consider a donation, to keep things rolling…thanks!
You're a kickass perl- or php-developer in the area
of vienna looking for a gig? get in contact!













hi !
i’ve tried your script but, i get errors:
Warning: require_once(HTTP/Client.php) [function.require-once]: failed to open stream: No such file or directory in ….
i checked source and see:
require_once ‘HTTP/Client.php’;
require_once ‘HTTP/Request.php’;
require_once ‘HTTP/Client/CookieManager.php’;
i’m missing these files in that zip pack.
go tho pear and dowload the packagges
as mentioned, the PEAR/HTTP-module which should include all 3 classes is a pre-requirement. on linux/*nix try these commands:
pear list
pear install HTTP
Here it is what i have instaled but i have the same error
missing 3 files.
xxxx@xxxx.biz [~]# pear list
Installed packages, channel pear.php.net:
=========================================
Package Version State
Archive_Tar 1.3.2 stable
Auth 1.5.4 stable
Auth_HTTP 2.1.6 stable
Auth_RADIUS 1.0.5 stable
Console_Getopt 1.2.3 stable
Crypt_CHAP 1.0.1 stable
DB 1.7.13 stable
File_Passwd 1.1.6 stable
File_SMBPasswd 1.0.2 stable
HTTP 1.4.0 stable
HTTP_Client 1.1.1 stable
HTTP_Download 1.1.3 stable
HTTP_Header 1.2.0 stable
HTTP_Request 1.4.2 stable
HTTP_Upload 0.9.1 stable
Log 1.9.11 stable
MDB 1.3.0 stable
MDB2 2.4.1 stable
MIME_Type 1.0.0 stable
Net_POP3 1.3.6 stable
Net_Socket 1.0.8 stable
Net_URL 1.0.15 stable
PEAR 1.6.2 stable
Structures_Graph 1.0.2 stable
System_Command 1.0.6 stable
XML_Parser 1.2.8 stable
XML_RPC 1.5.1 stable
hm, looks like php doesn’t have included pear in the include-path.
try to pass require_once the full path to the pear-libs, or check the include_path setting of your php.ini:
http://at2.php.net/manual/en/ini.core.php#ini.include-path
oh sorry, i have it installed now.
even when i go to run demo.php i get
Download done! File: demo.flv
can you a little bit explain how it does work ?
cuz i don’t see any video in my youtube acc.
what happens in this row:
$tube->download(”TWZ5j-SNVKs”,”demo.flv”);
what kind of extension of movies i can use ? or must i have all movies convert to flv before ?
hi miob…
sorry for the lack of inline documentation… I shall add some comments soon. briefly said there are two main methods (as you can see in demo.php):
1) Download of existing YouTube-clips
$tube->download(”TWZ5j-SNVKs”,”demo.flv”);
parameter 1 (”TWZ5j-SNVKs”) is the unique ID as you find it in the YouTube-URL of a particular video (”http://youtube.com/watch?v=TWZ5j-SNVKs”).
parameter 2 (”demo.flv”) is the filename the video is stored to locally
2) Upload of new Clips to YouTube
$id = $tube->upload (”/path/to/videofile.3gp”,”PHPTube Demo”, “Demo PHP YouTube”, “…”, 10, “DE”);
Parameters: local path to video-file, video-title, video-tags, description, category, language
Return: ID of the new Video-Clip
it’s important to initialize the class with you YouTube username & password to make the upload work!…
$tube = new PHPTube (”username”,”password”);
yeah perfect,
so in this case i use only:
$tube = new PHPTube (”user”,”pass”);
$id = $tube->upload (”videofile.3gp”,”PHPTube Demo”, “Demo PHP YouTube”, “…”, 10, “DE”);
i got response “No URL given.”
videofile.3gp file exists in same directory as is demo.php
i veven noticed that if i put wrong pass word to youtube it doesn’t tell me that i was wrong…
let me know what i’m wrong
hi again..
Im pretty sure this missleading error-message is a result of wrong username/password (I’ve just tried it mysel). I’ve bugfixed this in 0.1.1 …
thx!
Perfect, i updated latest version
my resut was:
Upload done! ID: BB7AaDZ20bs
i checked it on the site and found it.
can you tell me how do i get embedded code if i got id ?
and tell me please what extensions i can upload to youtube…
thanx
cool, glad it worked!
about the supported file-formats: check this: http://www.google.com/support/youtube/bin/answer.py?answer=55744&topic=10526
in addition it seems to work with 3GP and some other formats…
about the embedded code: just use the normal embed-code from any other video, just be sure to replace both occurences of the ID (f.e. “rYOxP_j64vQ”) with the new ID!
AWESOME !!!
Very excellent job !
i see you tube allows to upload up to 100mb, i must handle it on my server, do you know any utility what to show to user while he’s waiting for upload , something nice..
i like dailymotion.com upload graphics, it’s kicking ass, do you know something nice ?
i think i should donate , with your help
you might wanna search for flash+file+upload to find some ready-to-use flash solutions for file uploads…
http://www.google.com/search?q=flash%2Bfile%2Bupload
Thx for the script. It works! But when I try to upload a video little bigger(23MB), i have the following warning:
Maximum execution time of 30 seconds exceeded in phptube.php on line 97
Is there anyway to fix it?
@inthesky: increase the setting max_execution_time in your php.ini (usually somewhere like /etc/php5/apache2/php.ini). dont forget to restart apache!
if you cant edit php.ini (like if you’re no root/admin) you might wanna try ini_set:
http://at.php.net/manual/en/function.ini-set.php
thx:)
hi again! I created a upload page in my site. But when i upload, it gives me the following warning (but i get a video ID returned):
A PHP Error was encountered
Severity: Warning
Message: fread() [function.fread]: Length parameter must be greater than 0
Filename: HTTP/Request.php
Line Number: 945
(For uploading the video from client-side to my server, i use php form and put the video in a certain place for using your script.)
Do you know what is the problem?
ah, for the ini_set, i tried it for a public php server, but it seems doesn’t work. Can a server be configurated to reject it?
@inthesky: sorry, without seeing the code I really can’t tell where’s the problem.
regarding ini_set: yes, It’s quite likely that public/shared servers disable some of the php.ini-settings for normal users…
Hello,
I am using PHPTube..
at first it worked very well…but now I am getting “L PUBLIC” as the ID of the File
I have uploaded…does anybody know why I am getting this.
Thanks again Guys..
Elie
hi elie, I just investigated on the problem and found the reason… YouTube apparantly has slightly changed their upload-forms (which my class relies on)…PHPTube 0.1.2 should fix the problem!
Thanks “subnet”. I will test it and then brief you on the results….
Thanks again for the effort…and keep up the good job..
Elie
[...] a side-effect of this update, some commenters noticed that my video upload-API PHPTube has been broken by these changes. todays release 0.1.2 is [...]
hi, i have changed the new version and it works well. thanks. And I would like to make my page more beautiful. Do you know if youtube gives an API for feeding back the rate of progress for the upload?
@intehsky: well done!
unfortunately there’s no way to get the upload-progress when uploading to youtube
just curious, where do I find your site?
btw.: if you like PHPTube, don’t forget to vote for it at http://www.phpclasses.org/vote.html - your vote is appreciated very much! (you need to sign up for phpclasses to vote…which is really worth the effort as its a constantly good source for php-code).
[...] noticed that my YouTube-hack PHPTube has been nominated for June’s PHP Innovation Award at PHP Classes, cool! weither you’re [...]
Thank you for that great class!!
it’s a good class, but the big problem with it is it’s requirness ! of pear, and not
everybody have it
hm, I thought PEAR is pretty much standard nowadays…I might be wrong though…
hi
i have downloaded the 0.1.2 version of the class from top of this page and i am getting back ‘L PUBLIC ‘.
Has youtube changed again or is this link not to new version ?
Please help.
Best Regards
Richard Moss
@rick moss: the link above should be correct, to make sure check phptube.php for the exact version-info.
also, I just tried it with the supplied demo.3gp-file and it everything seems ok. maybe it’s a matter of your video-file (too large?), my class currently doesn’t parse error message from youtube.
no probs got it working now
thanks
great class !
does this require PHP 5 ? I am running 4.4.6 and get the following error:
http://media.d1tv.net/mark/youtube/phptube.php
thanks
markus
you’re right markus, there are some syntax-problems with PHP 4.x - version 0.1.3 (link above) should fix this… thx!
Hello everyone, I’m wondering how do I upload videos onto youtube. I have a Youtube account but It is so confusing, I Click the Upload at the top right corner of the site but it doesn’t work.
Please give me a reason or how i can do it properly
sorry, derek, if you’re looking for support using youtube, this blog is probably the wrong place
… you might wanna try youtube’s helpcenter - http://www.google.com/support/youtube/?hl=en_US
Hi, it’s possible to add a progress bar? i don’t know using a progress script?
Hi again, i added my vote to your phpTube, thaks for you code and works perfect but how can i add a progress bar? can i use a flash uploader with your phpTube? | like the original YouTube uploader | thanks and i will wait for your response and good look with the contest.
there are more scripts for sale on the internet
try search on google…
e.g. this:
http://www.element-it.com/DEMOMULTIPOW.ASPX
even some free if you know flash..
http://www.codeproject.com/aspnet/FlashUpload.asp
emm… yes i know but how can i get the upload progress? it’s possible? i use PHP..
thank’s
@orlando: it depends:
a) do you want a progress bar when users are uploading stuff to _your_ server - go with one of the linked solutions
b) do you want a progress bar when youre uploading to _youtbue.com_ - I’m pretty sure that’s impossible…
b) when upload to youtube, but i see in YouTube API support that exist an API to know the uploading progress
can you give me the link to that information?
I’m wondering, since I though YouTube doesn’t provide any API f. uploads, yet…
how to install PEAR/HTTP-module for windows?
windows-LAMP packages already contain PEAR, look for go-pear.bat. otherwise you might read:
http://pear.php.net/manual/en/installation.getting.php
i also have that problem. Where can I dind these files?
require_once ‘HTTP/Client.php’;
require_once ‘HTTP/Request.php’;
require_once ‘HTTP/Client/CookieManager.php’;
as already mentioned above, all three files are part of the PEAR/HTTP-module. on linux/*nix try these commands:
pear list
pear install HTTP
[quote]how to install PEAR/HTTP-module for windows?[/quote]
lo mismo me pregunto
I have your phptube working now on my site.
Its working excellent.
thanks
cool…if it’s a public site, drop us the url!
oh and by the way congratualtions on the php award
i would love to show you it working but unfortunately its in a non public area of my site.
sorry
congratualations on the php award by the way.
deserved !
thx!
how to setting PEAR in local web server? I try it, but fail.
Hi,
Sorry I have got a basic question. when we give upload and download interface from our website to upload to youtube whose bandwidth will be consumed?. even though we upload to youtube directly does our web hosting server bandwidth consumed?
I guess we are making a direct socket connection from client to youtube so our bandwidth should not be used right?
please clarify.
thanks
Chandra
not sure if I understand you correct, but if you’re gonna use phptube on your server for relaying uploads to youtube, you’ll consume bandwidth for every video twice: first, when uploading from the client to your server, and second when transferring the video from your server to youtube using phptube. playback-bandwidth of course is handled by youtube
does that answer your question?
how to install PEAR/HTTP-module for windows?
look out for a LAMP-distribution like XAMPP - this should already contain PEAR. look for go-pear.bat. otherwise you might read:
http://pear.php.net/manual/en/installation.getting.php
but I’ve already mentioned that above