gpt4 book ai didi

php - PHP YouTube API,获取上传状态

转载 作者:行者123 更新时间:2023-12-03 06:31:44 25 4
gpt4 key购买 nike

我花了一点时间来尝试将已上传视频的状态发送到YouTube。我按照下面的URL设置了CRON作业,该作业会将视频发送到YouTube,并得到响应;最好使用YouTube ID,以便将其保存在数据库中。不利的一面是我无法使它正常工作。

http://framework.zend.com/manual/1.12/en/zend.gdata.youtube.html

我的代码:(基本上是上述网址的复制和过去)

    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";
}
}
}
}

上面的方法在所有方面都起作用,减去了我总是得到“无法检索视频状态信息...”的事实。我究竟做错了什么?我一直盯着这个看了好几个小时,所以我想像它是我错过的简单事物。

最佳答案

我并没有很困难地完成这项工作。答案是将所有返回代码替换为(自定义了一点,因为我需要一个返回值,因为这是一个CakePHP组件。):

        $state = $newEntry->getVideoState();

if ($state) {
$response['id'] = $newEntry->getVideoId();
} else {
$response['error'] = "Not able to retrieve the video status information yet. " .
"Please try again later.\n";
}

return $response;

关于php - PHP YouTube API,获取上传状态,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19851505/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com