【问题标题】:Youtube API PHP Zend GData Direct UploadYoutube API PHP Zend GData 直接上传
【发布时间】:2010-10-21 19:30:40
【问题描述】:

我正在创建一个网络应用程序,它代表用户将网站从我的服务器上传到 youtube。

我已按照 Google 的 PHP 文档进行操作,并成功获得了会话令牌。

这是我用来检索会话令牌的代码:

require_once 'Zend/Loader.php';
Zend_Loader::loadClass('Zend_Gdata_AuthSub');

$singleUseToken = $_GET['token'];

$sessionToken = Zend_Gdata_AuthSub::getAuthSubSessionToken($singleUseToken);  

echo $sessionToken;

我的意图是将每个用户的会话令牌存储在数据库中。一个单独的脚本将在代表用户直接上传视频之前检索该会话令牌。

这是我被绊倒的地方。目前上传视频的文档直接使用如下代码:

require_once 'Zend/Loader.php';
Zend_Loader::loadClass('Zend_Gdata_AuthSub');

$yt = new Zend_Gdata_YouTube($httpClient);  

$myVideoEntry = new Zend_Gdata_YouTube_VideoEntry(); 

$filesource = $yt->newMediaFileSource('somefile.mp4'); 
$filesource->setContentType('video/mp4'); 
$filesource->setSlug('somefile.mp4');  
$myVideoEntry->setMediaSource($filesource);  
$myVideoEntry->setVideoTitle('My Test Movie'); 
$myVideoEntry->setVideoDescription('My Test Movie'); 

$myVideoEntry->setVideoCategory('Entertainment');  
$myVideoEntry->SetVideoTags('test');  
$myVideoEntry->setVideoDeveloperTags(array('test', 'tester'));  

$uploadUrl = 'http://uploads.gdata.youtube.com/feeds/api/users/default/uploads';  

$newEntry = $yt->insertEntry($myVideoEntry, $uploadUrl, 'Zend_Gdata_YouTube_VideoEntry');

我对如何正确制定以下部分的 $httpClient 变量感到困惑:

$yt = new Zend_Gdata_YouTube($httpClient);

我已经将会话令牌存储在我的数据库中,需要跳过身份验证,只需使用我的会话令牌执行直接上传。

感谢任何帮助。

谢谢, 泰根·斯奈德

【问题讨论】:

    标签: php zend-framework youtube-api


    【解决方案1】:

    我能够找出解决方案。如果有人对这里感兴趣,那就是我的代码。

    <?php
    $clientLibraryPath = '/library';
    $oldPath = set_include_path(get_include_path() . PATH_SEPARATOR . $clientLibraryPath);
    
    require_once 'Zend/Loader.php';
    Zend_Loader::loadClass('Zend_Gdata_AuthSub');
    Zend_Loader::loadClass('Zend_Gdata_YouTube'); 
    Zend_Loader::loadClass('Zend_Uri_Http');
    
    $sessionToken = '[whatever this is]';
    $developerKey = '[whatever this is]';
    
    $httpClient = new Zend_Gdata_HttpClient();
    $httpClient->setAuthSubToken($sessionToken);
    
    $yt = new Zend_Gdata_YouTube($httpClient, '23', '234', $developerKey);
    
    $myVideoEntry = new Zend_Gdata_YouTube_VideoEntry();
    
    $file= '../path_to_file/filename.mp4';
    $file = realpath($file);  
    
    $filesource = $yt->newMediaFileSource($file); 
    $filesource->setContentType('video/mp4'); 
    $filesource->setSlug($file);  
    $myVideoEntry->setMediaSource($filesource);  
    $myVideoEntry->setVideoTitle('My Test Movie'); 
    $myVideoEntry->setVideoDescription('My Test Movie'); 
    
    $myVideoEntry->setVideoCategory('Entertainment');  
    $myVideoEntry->SetVideoTags('testme');  
    $myVideoEntry->setVideoDeveloperTags(array('tester', 'test'));  
    
    $uploadUrl = 'http://uploads.gdata.youtube.com/feeds/api/users/default/uploads';  
    
    
    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();
    
    }
    
    ?>
    

    【讨论】:

    • 我能够获取此代码并对其进行修改以在课堂环境中使用它。好代码,干得好。
    猜你喜欢
    • 2011-06-14
    • 2011-05-24
    • 2010-10-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-03-30
    • 1970-01-01
    相关资源
    最近更新 更多