我正在尝试获取上传到 YouTube 的视频的状态。我按照下面的 URL 设置了一个将视频发送到 YouTube 的 CRON 作业,得到响应;最好使用 YouTube ID,这样我就可以将其保存在数据库中。不利的一面是我无法让它工作。
http://framework.zend.com/manual/1.12/en/zend.gdata.youtube.html
我的代码:(基本上是从上面的 URL 复制过去)
function upload($filename, $options = array()) {
$default = array_merge(
array(
'username' => 'USERNAME',
'password' => 'PASSWORD',
'service' => 'youtube',
'client' => null,
'source' => 'YouTube Component',
'loginToken' => null,
'loginCaptcha' => null,
'authenticationURL' => 'https://www.google.com/accounts/ClientLogin',
'applicationId' => 'YouTube Component',
'clientId' => 'YouTube Component',
'developerKey' => 'DEVELOPERS-KEY',
'content_type' => 'video/quicktime',
'title' => null,
'description' => null,
'category' => null,
'tags' => null,
),
(array)$options
);
extract($default);
$this->controller->Zend->loadClass('Zend_Gdata_YouTube');
$this->controller->Zend->loadClass('Zend_Gdata_ClientLogin');
$httpClient = Zend_Gdata_ClientLogin::getHttpClient(
$username,
$password,
$service,
$client,
$source,
$loginToken,
$loginCaptcha,
$authenticationURL
);
$yt = new Zend_Gdata_YouTube($httpClient, $applicationId, $clientId, $developerKey);
$myVideoEntry = new Zend_Gdata_YouTube_VideoEntry();
$filesource = $yt->newMediaFileSource($filename);
$filesource->setContentType($content_type);
$filesource->setSlug($filename);
$myVideoEntry->setMediaSource($filesource);
$myVideoEntry->setVideoTitle($title);
$myVideoEntry->setVideoDescription($description);
$myVideoEntry->setVideoCategory($category);
$myVideoEntry->SetVideoTags($tags);
$myVideoEntry->setVideoPrivate();
$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();
}
try {
$control = $myVideoEntry->getControl();
} catch (Zend_Gdata_App_Exception $e) {
echo $e->getMessage();
}
if ($control instanceof Zend_Gdata_App_Extension_Control) {
if ($control->getDraft() != null && $control->getDraft()->getText() == 'yes') {
$state = $myVideoEntry->getVideoState();
if ($state instanceof Zend_Gdata_YouTube_Extension_State) {
print 'Upload status: ' . $state->getName() .' '. $state->getText();
} else {
print 'Not able to retrieve the video status information' .' yet. ' . "Please try again shortly.\n";
}
}
}
}
以上方法在各个方面都有效,减去我总是得到“无法检索视频状态信息......”的事实。我究竟做错了什么?我已经盯着这个看了好几个小时了,所以我想这是我错过的一些简单的事情。