【问题标题】:Youtube API - PHP - How to modify the start time of an existing broadcast?Youtube API - PHP - 如何修改现有广播的开始时间?
【发布时间】:2017-10-11 19:45:58
【问题描述】:

我已经可以在 YouTube Live 中创建广播事件,现在我正在尝试使用更新 API 修改现有事件。

问题是 VideoSnippet 库不允许这样做,必须以其他方式完成。

(这个PHP函数将由前端通过ajax请求调用)

function updateBroadcast(){
    if(!isset($client)){
        $client = getClient();
    }
    $streamData = $_POST['streamData'];

    $client->setAccessToken($_SESSION['google_access_token']);
    $service =  new Google_Service_YouTube($client);

    if ($client->getAccessToken()) {

        try {
            $videoId = $streamData['id'];

            // Call the API's videos.list method to retrieve the video resource.
            $listResponse = $service->videos->listVideos("snippet",
                array('id' => $videoId));

            if (empty($listResponse)) {
                return  json_encode(sprintf('Can\'t find a video with video id: %s', $videoId));
            } else {
                // Since the request specified a video ID, the response only
                // contains one video resource.
                $video = $listResponse[0];
                $videoSnippet = $video['snippet'];

                $videoSnippet->setTitle($streamData['eventName']);
                $videoSnippet->setDescription($streamData['eventCategory']);
                $videoSnippet->setScheduledStartTime($streamData['eventDateTime'])
            }

        } catch (Google_Service_Exception $e) {
            echo sprintf('<p>A service error occurred: <code>%s</code></p>',
                htmlspecialchars($e->getMessage()));
        } catch (Google_Exception $e) {
            echo sprintf('<p>An client error occurred: <code>%s</code></p>',
                htmlspecialchars($e->getMessage()));
        }
        return json_encode("Video Updated");
    }
}

它会抛出这个错误:

致命错误:在第 242 行的 /var/www/html/production/app/empowerir/php/videoStreaming/functions.php 中调用未定义的方法 Google_Service_YouTube_VideoSnippet::setScheduledStartTime()

第 242 行是:

$videoSnippet->setScheduledStartTime($streamData['eventDateTime'])

【问题讨论】:

    标签: php youtube-api


    【解决方案1】:

    如果您阅读了错误,它应该已经告诉您问题所在:

    调用未定义的方法 Google_Service_YouTube_VideoSnippet::setScheduledStartTime()

    看看LiveBroadcasts.update,没有setScheduledStartTime这样的方法(看起来你是在飞行中发明的)。如果您想更新 snippet.scheduledStartTime 属性,则必须在您的 LiveBroadcasts.update 请求的请求正文中进行设置。

    【讨论】:

    • 问题是:“如何使用 YouTube 的 PHP 库更新直播?”。如何调用更新方法?如何传递参数?
    • 我不能为你写代码,因为我不使用 PHP。我希望你知道如何使用你的工具,而不是在飞行中发明方法xD
    猜你喜欢
    • 1970-01-01
    • 2013-10-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-06-30
    • 1970-01-01
    • 2014-05-29
    相关资源
    最近更新 更多