【问题标题】:CodeIgniter: Upload Video to YouTubeCodeIgniter:将视频上传到 YouTube
【发布时间】:2012-03-07 14:27:04
【问题描述】:

我正在使用 CodeIgniter + Zend 库。我想让用户将视频上传到我的网站,然后我会将其上传到我的 YouTube 频道。这是我第一次接触 YouTube API。有人能指出我正确的方向吗?

我看这个是对的吗:http://code.google.com/apis/youtube/2.0/developers_guide_protocol.html#Direct_uploading?有没有人有显示如何通过 PHP 完成上传的 sn-p 代码?

【问题讨论】:

    标签: php youtube-api codeigniter-2 gdata-api


    【解决方案1】:

    试试它的工作原理

    class Addvideo extends Controller {
    
    
    
        function Addvideo()
    
        {
    
            parent::Controller();
    
    
            $this->load->library("zend");
            $this->zend->load("Zend/Gdata/AuthSub");
            $this->zend->load("Zend/Gdata/ClientLogin");
            $authenticationURL= 'https://www.google.com/youtube/accounts/ClientLogin';
    
            /************Authentication**************/
    
            $this->zend->load("Zend/Gdata/YouTube");
            $this->zend->load("Zend/Gdata/HttpClient");
    
            $this->zend->load("Zend/Gdata/App/MediaFileSource");
            $this->zend->load("Zend/Gdata/App/HttpException");
            $this->zend->load("Zend/Uri/Http");
    
    
            $youtube = new Zend_Gdata_YouTube();
            $myVideoEntry = new Zend_Gdata_YouTube_VideoEntry();
            $Gdata_AuthSub=new Zend_Gdata_AuthSub();
    
            $this->load->helper("text");
        }
    
            function index(){
                $data['result']='videos';
                $this->load->view('videos/newform', $data); 
    
            }
    
        function indexed(){
    
            $title = 'GruppoDSE Video';
            $description = 'Description';
    
            $youtube = new Zend_Gdata_YouTube();    
            $authenticationURL= 'https://www.google.com/youtube/accounts/ClientLogin';
            $httpClient = Zend_Gdata_ClientLogin::getHttpClient(
                    $username = $this->config->item('username'),
                    $password = $this->config->item('password'),
                    $service = 'youtube',
                    $client = null,
                    $source = 'Arts Connector', // a short string identifying your application
                    $loginToken = null,
                    $loginCaptcha = null,
                    $authenticationURL); 
    
            $developerKey = $this->config->item('developer_key');
            $applicationId = 'Arts Connector';
            $clientId = 'My video upload client - v1';
            $yt = new Zend_Gdata_YouTube($httpClient, $applicationId, $clientId, $developerKey);
    
            // create a new VideoEntry object
            $myVideoEntry = new Zend_Gdata_YouTube_VideoEntry();
    
            $myVideoEntry->setVideoTitle($title);
            $myVideoEntry->setVideoDescription($description);
            // The category must be a valid YouTube category!
            $myVideoEntry->setVideoCategory('Autos');
    
            // Set keywords. Please note that this must be a comma-separated string
            // and that individual keywords cannot contain whitespace
            $myVideoEntry->SetVideoTags('cars, funny');
    
            $tokenHandlerUrl = 'http://gdata.youtube.com/action/GetUploadToken';
            $tokenArray = $yt->getFormUploadToken($myVideoEntry, $tokenHandlerUrl);
            $data['tokenValue'] = $tokenArray['token'];
            $data['postUrl'] = $tokenArray['url']; 
    
    
                // place to redirect user after upload
                $data['nextUrl'] = 'http://www.avantajsoftwares.com/gruppo/uploadVideo';
                $res="Il file video aggiungere con successo, il risultato atteso verrà visualizzato dopo alcune volte";
                $this->session->set_userdata('greenFlag',$res);
                // build the form
    
                $this->load->view('videos/form', $data);
    
            }
    
        function AddVideoAjax(){
    
                 $title=$_POST['title'];
                 $description=$_POST['description'];
    
            $youtube = new Zend_Gdata_YouTube();    
            $authenticationURL= 'https://www.google.com/youtube/accounts/ClientLogin';
            $httpClient = Zend_Gdata_ClientLogin::getHttpClient(
                    $username = $this->config->item('username'),
                    $password = $this->config->item('password'),
                    $service = 'youtube',
                    $client = null,
                    $source = 'Arts Connector', // a short string identifying your application
                    $loginToken = null,
                    $loginCaptcha = null,
                    $authenticationURL); 
    
            $developerKey = $this->config->item('developer_key');
            $applicationId = 'Arts Connector';
            $clientId = 'My video upload client - v1';
            try {
            $yt = new Zend_Gdata_YouTube($httpClient, $applicationId, $clientId, $developerKey);
    
            // create a new VideoEntry object
            $myVideoEntry = new Zend_Gdata_YouTube_VideoEntry();
    
            $myVideoEntry->setVideoTitle($title);
            $myVideoEntry->setVideoDescription($description);
            // The category must be a valid YouTube category!
            $myVideoEntry->setVideoCategory('Education');
    
            // Set keywords. Please note that this must be a comma-separated string
            // and that individual keywords cannot contain whitespace
            $myVideoEntry->SetVideoTags('Seminar, Events');
    
            $tokenHandlerUrl = 'http://gdata.youtube.com/action/GetUploadToken';
            $tokenArray = $yt->getFormUploadToken($myVideoEntry, $tokenHandlerUrl);
            $tokenValue = $tokenArray['token'];
            $postUrl = $tokenArray['url']; 
    
    
                // place to redirect user after upload
                $nextUrl = 'http://www.avantajsoftwares.com/gruppo/Addvideo';
                $res="Il file video aggiungere con successo, il risultato atteso verrà visualizzato dopo alcune volte";
                $this->session->set_userdata('greenFlag',$res);
                // build the form
    
                echo  $form = '<form action="'. $postUrl .'?nexturl='. $nextUrl .
                        '" method="post" enctype="multipart/form-data" onsubmit="return valid()">'. 
                        '<div style="float:left;"><input name="file" type="file" id="file"/></div><div class="preloader"></div>'. 
                        '<input name="token" type="hidden" value="'.  $tokenValue .'"/>'.
                        '<div style="clear:both"></div>'.
                        '<div class="login-footer" id="prog_bar style="cursor:pointer"><input value="Carica video" type="submit" id="validate" class="button" onclick="progress_bar()"/></div>'. 
                        '</form>';
    
    
    
                } catch (Zend_Gdata_App_Exception $e) {
    
                    echo $return="<div class='login-footer' style='width:130px;'><a style='height:16px; padding-top:7px;' class='button' href=".site_url()."Addvideo>Riprova di nuovo</a></div>";
                }
                // Assuming that $videoEntry is the object that was returned during the upload
                 //$state = $myVideoEntry->getVideoState();
    
    
    
    
            }
    
        function getAuthSubRequestUrl()
            {
                $gdata_AuthSub = new Zend_Gdata_AuthSub();
                $next = 'http://www.avantajsoftwares.com/gruppo/videos';
                $scope = 'http://gdata.youtube.com';
                $secure = false;
                $session = true;
                return $data=$gdata_AuthSub->getAuthSubTokenUri($next, $scope, $secure, $session);
            }
    
        function getAuthSubHttpClient()
            {
                echo $_SESSION['sessionToken'];
    
                if (!isset($_SESSION['sessionToken']) && !isset($_GET['token']) ){
                    echo '<a href="' . $this->getAuthSubRequestUrl() . '">Login!</a>';
                    return;
                } else if (!isset($_SESSION['sessionToken']) && isset($_GET['token'])) {
                  $_SESSION['sessionToken'] = Zend_Gdata_AuthSub::getAuthSubSessionToken($_GET['token']);
                }
    
                $httpClient = Zend_Gdata_AuthSub::getHttpClient($_SESSION['sessionToken']);
                return $httpClient;
            }
    
    
        /*************************** Class end ***********************************/
    }
    

    【讨论】:

      【解决方案2】:

      我不建议将 Zend 与 CI 结合使用,因为它的大小和复杂性。我专门为 CodeIgniter 开发了一个库https://github.com/jimdoescode/CodeIgniter-YouTube-API-Library

      该库提供了几种不同的上传到 YouTube 的选项。您可以直接上传,将存储在服务器上的视频上传到 youtube。还可以选择使用表单将视频从客户端上传到 youtube。如果您有任何问题,请查看并告诉我。

      【讨论】:

        【解决方案3】:

        文档包含 Zend Gdata 的此方法以及其他方法:Uploading Videos

        【讨论】:

        • Martti - 演示似乎解释了如何上传到用户的 YouTube 帐户。我希望用户上传到我的 YouTube 帐户。
        • 您可以通过使用ClientLogin将您的登录详细信息嵌入到源中来仅验证一个帐户。
        • @MarttiLaine,我认为你是对的。我认为这样的事情应该是可能的。
        • @StackOverflowNewbie 我没有理由不这样做:它是在 Zend Gdata 中实现的,它是验证单个帐户的唯一方法。文档建议不要这样做,因为这些天建议通过直接用户名/密码以外的其他方式处理身份验证,但在您的情况下,这是唯一的机会。
        猜你喜欢
        • 2015-02-01
        • 2011-10-30
        • 1970-01-01
        • 2012-11-01
        • 2015-01-10
        • 2011-11-30
        • 2020-05-12
        • 2019-09-24
        • 2012-04-07
        相关资源
        最近更新 更多