文件过早结束,直接上传youtube

时间:2013-08-29 16:36:10

标签: php zend-framework file-upload youtube youtube-api

我正在尝试使用直接上传方法将视频上传到youtube,如here, at documentation所述。

我已正确包含了我的开发人员密钥,并且我也没有任何问题地获取会话令牌。当它将我引导到带有令牌的上传页面时,会出现问题。它不是通过视频ID上传和返回,而是向我显示一行,

  

文件过早结束。

我是第一次使用youtube api的新手,我设法完成身份验证和很少的错误,但是我不知道,为什么或在哪里出现这个问题。

这是我的php文件,

<?php

session_start();

require_once 'Zend/Loader.php'; // the Zend dir must be in your include_path
Zend_Loader::loadClass('Zend_Gdata_YouTube');
Zend_Loader::loadClass('Zend_Gdata_AuthSub');
Zend_Loader::loadClass('Zend_Uri_Http');


function getAuthSubRequestUrl()
{
    $next = 'http://localhost/trial/trial.php';
    $scope = 'http://gdata.youtube.com';
    $secure = false;
    $session = true;
    return Zend_Gdata_AuthSub::getAuthSubTokenUri($next, $scope, $secure, $session);
}

//generate link if no session or token has been requested
if (!isset($_SESSION['sessionToken']) && !isset($_GET['token'])){

  echo '<a href="' . getAuthSubRequestUrl() . '">Login to YouTube API!</a>';

  //if token has been requested but not saved to a session then save the new token to a session 
} else if (!isset($_SESSION['sessionToken']) && isset($_GET['token'])) {

  $_SESSION['sessionToken'] = Zend_Gdata_AuthSub::getAuthSubSessionToken($_GET['token']);

}


if(isset($_SESSION['sessionToken'])) {

    $clientLibraryPath = '/library';
    $oldPath = set_include_path(get_include_path() . PATH_SEPARATOR . $clientLibraryPath);

    $sessionToken = $_SESSION['sessionToken'];
    $developerKey = 'my-key-here';   //I have inserted the key correctly.

    $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); 

   // create a new Zend_Gdata_App_MediaFileSource object
   $filesource = $yt->newMediaFileSource('file.mov');
   $filesource->setContentType('video/quicktime');
   // set slug header
   $filesource->setSlug('file.mov');
   $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();

    }
}
?>

感谢。如有需要,请询问有关问题或代码的进一步说明或信息。

1 个答案:

答案 0 :(得分:1)

我建议改用Data API并使用可恢复的上传。 Here's a PHP upload example