2012-07-01 69 views
1

我要上傳與未來路視頻:Yii + Zend gdata。的Youtube上傳

  1. 我只是上傳文件到服務器(像往常一樣)
  2. 我的服務器端Yii開發的應用程序需要從一個特殊的YouTube上的視頻,並上傳帳戶在YouTube上

我有什麼:

  • 我的YouTube(谷歌)帳戶名和電子郵件。 「名」或「[email protected]
  • 我的密碼
  • 開發人員密鑰,這是我在谷歌的「產品儀表板」
  • 的應用程序,它的名字「MYAPP」的名稱發現:

Product Dashboard: myapp

所以,我讀了一些谷歌文檔和決定,對我來說最好的辦法是使用ClientLogin認證類型,因爲我只有一個賬號使用,我擁有所有必要的數據。我找到了一個ZendFramework的GData的例子,並將其導入到我的Yii應用程序中。

我特意簡化了代碼,只是爲了從/ upload目錄上傳一個視頻來測試它的工作。我希望能夠上傳我的YT帳戶中的視頻。當然,沒有視頻,在這裏我:-)行動的完整代碼如下:

Yii::import('application.vendors.*'); 
require_once 'Zend/Loader.php'; 
Zend_Loader::loadClass('Zend_Gdata_YouTube'); 
Zend_Loader::loadClass('Zend_Gdata_ClientLogin'); 

$yt_user = 'myYTname'; 
$yt_pass = 'myYTpass'; 
$yt_source = 'myapp'; 
$yt_api_key = 'veryVERYlongKEYhere'; 

$authenticationURL= 'https://www.google.com/accounts/ClientLogin'; 
$httpClient = Zend_Gdata_ClientLogin::getHttpClient(
    $username = $yt_user, 
    $password = $yt_pass, 
    $service = 'youtube', 
    $client = null, 
    $source = $yt_source, 
    $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(Yii::getpathOfAlias('webroot').'/upload/videos/video.mp4'); 
$filesource->setContentType('video/mp4'); 
$filesource->setSlug('video.mp4'); 
$myVideoEntry->setMediaSource($filesource); 
$myVideoEntry->setVideoTitle('My Test Movie'); 
$myVideoEntry->setVideoDescription('My Test Movie description'); 
$myVideoEntry->setVideoCategory('Autos'); 
$myVideoEntry->SetVideoTags('cars, funny'); 
$myVideoEntry->setVideoDeveloperTags(array('mydevtag', 'anotherdevtag')); 

$uploadUrl = "http://uploads.gdata.youtube.com/feeds/api/users/{$yt_user}/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(); 
} 

正如你所看到的,有來自官方的例子有很多默認的代碼。但它不起作用。沒有人回聲顯示我的信息。但是,當我刪除的try-catch,我得到了一個錯誤:

Zend_Gdata_App_HttpException 
Read timed out after 10 seconds 
+0

UPD 1:我通過在ZendHTTP類中設置新的超時參數來移除超時異常。所以現在我的應用程序的頁面只是加載加載,然後顯示nithong。 – Apfel

+0

UPD 2:我試圖上傳的視頻是300 Kb的大小,所以一定不需要傳輸數據,甚至10秒 – Apfel

+0

UPD 3:我用XDebug調試了我的應用程序,發現有仍然愚蠢的超時豁免。 100秒超時上傳一個300Kb的文件...有什麼問題,但我不明白什麼... – Apfel

回答

0

所以,這個問題是由我自己:) 首先來解決:不要試圖從本地主機上傳! 然後在我的情況下,我得到一個錯誤,我沒有說我的開發密鑰!所以,如果你得到了同樣的錯誤,試圖改變這一點:

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

通過將4個參數 - 額外的頭:

$yt->insertEntry($myVideoEntry, $uploadUrl, 'Zend_Gdata_YouTube_VideoEntry', array(
    'X-GData-Key' => 'key=yourBIGbigBIGdeveloperKEYhere' 
)); 

祝你好運,有樂趣與YouTube API!