PHP YouTube API,获取上传状态

时间:2013-11-08 04:17:44

标签: php cakephp zend-framework youtube

我正试图将上传的视频状态发送到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";
                }
            }
        }
    }

以上各方面都有效,减去了我总是得到“无法检索视频状态信息......”这一事实。我究竟做错了什么?我一直盯着这几个小时,所以我想象一下我错过了一些简单的东西。

1 个答案:

答案 0 :(得分:0)

我完成这件事并不是非常遥远。答案是将所有返回代码替换为(自定义,因为我需要一个返回值,因为这是一个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;