Wednesday, December 28, 2011

PHP: Upload a Video to YouTube using Zend Gdata and YouTube API

How to upload video to you tube?
1)Download Zend  
2)Include Zend Gdata Libs Zend folder (i.e., library\Zend)

//include Zend Gdata Libs 
  require_once("Zend/Gdata/ClientLogin.php"); 
  require_once("Zend/Gdata/HttpClient.php"); 
  require_once("Zend/Gdata/YouTube.php");
  require_once("Zend/Gdata/App/MediaFileSource.php"); 
  require_once("Zend/Gdata/App/HttpException.php"); 
   require_once('Zend/Uri/Http.php'); 
     
    //yt account info 
    $yt_user = 'username'; //youtube username or gmail account 
    $yt_pw = 'password'; //account password 
    $yt_source = 'Name'; //name of application (can be anything) 
     
    //video path 
  // $video_url = 'http://example.com/YouTubeVideoApp/'.$target; 
   $video_url = '/target/upload/'.$name; 
      
     
    //yt dev key 
    $yt_api_key = '<your youtube developer key>'; //your youtube developer key 
     
    //login in to YT 
    $authenticationURL= 'https://www.google.com/youtube/accounts/ClientLogin'; 
    $httpClient = Zend_Gdata_ClientLogin::getHttpClient( 
                                              $username = $yt_user, 
                                              $password = $yt_pw, 
                                              $service = 'youtube', 
                                              $client = null, 
                                              $source = $yt_source, // a short string identifying your application 
                                              $loginToken = null, 
                                              $loginCaptcha = null, 
                                              $authenticationURL); 
     
    $yt = new Zend_Gdata_YouTube($httpClient, $yt_source, NULL, $yt_api_key); 
     
    $myVideoEntry = new Zend_Gdata_YouTube_VideoEntry(); 
     
    $filesource = $yt->newMediaFileSource($video_url); 
    $filesource->setContentType('video/quicktime'); //make sure to set the proper content type. 
    $filesource->setSlug('mytestmovie.mov'); 
     
    $myVideoEntry->setMediaSource($filesource); 
     
    $myVideoEntry->setVideoTitle($title); 
    $myVideoEntry->setVideoDescription($desc); 
    // Note that category must be a valid YouTube category ! 
    $myVideoEntry->setVideoCategory('Entertainment'); 
     
    // Set keywords, note that this must be a comma separated string 
    // and that each keyword cannot contain whitespace 
    $myVideoEntry->SetVideoTags('Bridal, Bouquet, Designs'); 
     
    // Upload URI for the currently authenticated user 
     
    $uploadUrl = "http://uploads.gdata.youtube.com/feeds/users/$yt_user/uploads"; 
     
    // Try to upload the video, catching a Zend_Gdata_App_HttpException 
    // if availableor just a regular Zend_Gdata_App_Exception 
     
    try { 
        $newEntry = $yt->insertEntry($myVideoEntry, 
                                     $uploadUrl, 
                                     'Zend_Gdata_YouTube_VideoEntry'); 
    } catch (Zend_Gdata_App_HttpException $httpException) { 
        echo $httpException->getRawResponseBody(); 
    } catch (Zend_Gdata_App_Exception $e) { 
        echo $e->getMessage(); 
    } 
     
For more information see the YouTube Developer’s Guide 

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.