Here is an example how to generate YouTube video browser-based uploading form by PHP:

<?php
$youtube_email = "example@youtube.com"; // Change this to your youtube sign in email.
$youtube_password = "password"; // Change this to your youtube sign in password.
 
$postdata = "Email=".$youtube_email."&Passwd=".$youtube_password."&service=youtube&source=Example";
$curl = curl_init("https://www.google.com/youtube/accounts/ClientLogin");
curl_setopt($curl, CURLOPT_HEADER, "Content-Type:application/x-www-form-urlencoded");
curl_setopt($curl, CURLOPT_POST, 1);
curl_setopt($curl, CURLOPT_POSTFIELDS, $postdata);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 1);
$response = curl_exec($curl);
curl_close($curl);
 
list($auth, $youtubeuser) = explode("\n", $response);
list($authlabel, $authvalue) = array_map("trim", explode("=", $auth));
list($youtubeuserlabel, $youtubeuservalue) = array_map("trim", explode("=", $youtubeuser));
 
$youtube_video_title = "Example"; // This is the uploading video title.
$youtube_video_description = "Example"; // This is the uploading video description.
$youtube_video_category = "News"; // This is the uploading video category.
$youtube_video_keywords = "example, video"; // This is the uploading video keywords.
 
$data = '<?xml version="1.0"?>
            <entry xmlns="http://www.w3.org/2005/Atom"
              xmlns:media="http://search.yahoo.com/mrss/"
              xmlns:yt="http://gdata.youtube.com/schemas/2007">
              <media:group>
                <media:title type="plain">'.$youtube_video_title.'</media:title>
                <media:description type="plain">'.$youtube_video_description.'</media:description>
                <media:category
                  scheme="http://gdata.youtube.com/schemas/2007/categories.cat">'.$youtube_video_category.'</media:category>
                <media:keywords>'.$youtube_video_keywords.'</media:keywords>
              </media:group>
            </entry>';
 
$key = "adf15ee97731bca89da876c...a8dc"; // Get your key here: http://code.google.com/apis/youtube/dashboard/.
 
$headers = array("Authorization: GoogleLogin auth=".$authvalue,
                 "GData-Version: 2",
                 "X-GData-Key: key=".$key,
                 "Content-length: ".strlen($data),
                 "Content-Type: application/atom+xml; charset=UTF-8");
 
$curl = curl_init("http://gdata.youtube.com/action/GetUploadToken");
curl_setopt($curl, CURLOPT_USERAGENT, $_SERVER["HTTP_USER_AGENT"]);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_TIMEOUT, 10);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl, CURLOPT_POST, 1);
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
curl_setopt($curl, CURLOPT_REFERER, true);
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($curl, CURLOPT_HEADER, 0);
 
$response = simplexml_load_string(curl_exec($curl));
curl_close($curl);
?>
<script type="text/javascript">
  function checkForFile() {
    if (document.getElementById('file').value) {
      return true;
    }
    document.getElementById('errMsg').style.display = '';
    return false;
  }
</script>
 
<?php
$nexturl = "http://rubensargsyan.com"; // This parameter specifies the URL to which YouTube will redirect the user's browser when the user uploads his video file.
?>
 
<form action="<?php echo($response->url); ?>?nexturl=<?php echo(urlencode($nexturl)); ?>" method="post" enctype="multipart/form-data" onsubmit="return checkForFile();">
  <input id="file" type="file" name="file"/>
  <div id="errMsg" style="display:none;color:red">
    You need to specify a file.
  </div>
  <input type="hidden" name="token" value="<?php echo($response->token); ?>"/>
  <input type="submit" value="go" />
 
</form>
</php>
Share

17 Comments on Generate YouTube video browser-based uploading form by PHP

  1. Seth says:

    Is there a way to add an input field for the user to name the video before upload?

  2. Seth,

    I think there is, but sorry I can’t help you to find that way. I am too busy.

    Thanks.

  3. Biswanath Bose says:

    Hai, Sargsyan

    This code very much fruitful to me. By this i can upload the video in the you tube . Thanks to you.

    Biswanath bose

  4. Gerben Bol says:

    Hi,

    I’m using this as base for an upload form. I got a few questions. I got several people that can use this to upload video’s, but I want to check if they’re not harmfull before they’re online. I was thinking to make the video’s private so I can manually check them. Is it possible that the video’s are private automaticly?

  5. Alaa Sadik says:

    I can’t find the video in my YouTub videos, username, password and developer key are all correct.

  6. Hassi says:

    Hi, Thanks for providing the code it works great.
    Initially I was having problem that I was uploading videos but couldn’t find them in my channel and then I figured out following mistakes in my code:
    1) The video format was not supported by youtube (this can fail the video to upload in channel
    2) if you change the category that is not in youtube category list the upload will fail. Simple way to check that everything was process well in curl is to view the page source and see if Upload Token was issued with URL.. if its not issued then you are having problem with XML values.
    Cheers

  7. Jan says:

    Thank you very much for this lovely code! Is there some easy way to make uploaded videos be tagged as private? in the entry didn’t seem to work.
    Thanks again!

  8. Moe Sweet says:

    Hi,

    Which channel does the video go to?
    Site owner’s channel or uploader’s channel?

  9. zeeshan says:

    code is very useful, but mostly google has captcha issue,how you will handle this…?

  10. Tore says:

    Awesome code! Worked like a charm! I think it is the first time I tried to copy-and-paste something from the web and it worked from on the first try :) All clips landed perfectly in my Youtube account.
    Thank you for sharing! You are awesome! May you live long and get laid a lot!

  11. phphunger says:

    awesome tip….

  12. Thanks for the code. but I got a problem. When the video upload “invalid request” error. Please help me. Where the error am I doing?

  13. rakesh says:

    Hello Ruben!
    When i am using above code, then i get an error…
    Notice: Undefined offset: 1 in C:\xampp\htdocs\add_code\upload_on_youtube\check.php on line 18

    i am unable to find out solution. Please help me

    Thanks & regard
    Rakesh Kumar Singh

  14. Hildebrand Hilsenheim says:

    Really, thank you. Great post. For those who want to do automatic uploads, replace the first sentence by the second:

    <form action="url); ?>?nexturl=” method=”post” enctype=”multipart/form-data” onsubmit=”return checkForFile();”>

    You need to specify a file.

    <input type="hidden" name="token" value="token); ?>”/>

    url.”?nexturl=”.urlencode($nexturl));
    curl_setopt($ch, CURLOPT_HEADER, 0);
    curl_setopt($ch, CURLOPT_VERBOSE, 0);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_USERAGENT, $_SERVER[“HTTP_USER_AGENT”]);
    curl_setopt($ch, CURLOPT_POST, true);
    $post = array(
    “file”=>”@video.flv”,
    “token”=>$response->token
    );
    curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
    $response = curl_exec($ch);
    ?>

    I saw this solution at http://stackoverflow.com/quest.....ile-upload and it worked perfectly.

  15. Hildebrand Hilsenheim says:

    The code above is incomplete. I’m unable to put the code completely. But it is simple, just replace the form by the code of first reply of the link that I posted.

  16. munna says:

    pls send me full code of abv program it uploads d video automatically

  17. Linh says:

    i see it uploaded but i don’t see on my youtube
    Help me!

Leave a Reply

Info