1

我想从 Live Streaming API 获取最新的聊天消息。但我总是先得到最老的。我可以按相反的顺序得到它吗?我可以设置一个属性来实现这一点吗?

这是一些示例代码:

    if ($_GET['action'] == "listchatmessages")
    {
        $htmlBody .= '<h1>Chatnachrichten</h1>';
        $broadcastsChatResponse = $youtube->liveChatMessages ->listLiveChatMessages (
        $broadcastItem['snippet']['liveChatId'], 'snippet');
        $myfile = fopen("chatmessages.txt", "w") or die("Unable to open file!");
        $count = 0;
        if ($_GET['search'] != "")
        {
        if (!empty($broadcastsChatResponse['items']))
            {
                foreach ($broadcastsChatResponse['items'] as $broadcastChatItem)
                {
                    if ($count < 200)
                    {

                        $broadcastsDetailsChatResponse = $youtube->liveChatMessages ->listLiveChatMessages (
                        $broadcastChatItem['snippet']['liveChatId'], 'authorDetails');
                        $broadcastDetailsChatItem = $broadcastsDetailsChatResponse['items'][0];

                            if ((preg_match('/'.$_GET['search'].'/',$broadcastChatItem['snippet']['textMessageDetails']['messageText'])) OR $_GET['search'] == 'all')
                            {
                                $message = $broadcastChatItem['snippet']['authorChannelId'].'_'.$broadcastDetailsChatItem['authorDetails']['displayName'].'_'.$broadcastChatItem['snippet']['textMessageDetails']['messageText']."\n";
                                $htmlBody .= '<li> Author: <a href="'.$broadcastChatItem['snippet']['authorChannelId'].'">'.$broadcastDetailsChatItem['authorDetails']['displayName'].'</a> schrieb: '.$broadcastChatItem['snippet']['textMessageDetails']['messageText'].'</li>';
                                $count++;
                                fwrite($myfile, $message);
                            }
                    }
                    else
                    {
                        break;
                    }
                }


                $htmlBody .= '<h4> Alle Chatnachrichten empfangen (Anzahl der teilnehmenden Nahcrichten: '.$count.')</h4>';
            }
            else
            {
                $htmlBody .= '<p>Der Stream scheint nicht online zu sein</p>';
            }
        }
        else
        {
            $htmlBody .= 'Kein Suchwort. Nutze all um alle Nachrichten zu nehmen';
        }

        fclose($myfile);
    }
4

1 回答 1

1

似乎没有参数可以传递给 liveChatMessages/list端点的请求以首先返回最新消息。

您必须下载所有消息,并按每条聊天消息下的 ISO 8601 格式日期对它们进行排序snippet.publishedAt

如果您希望在 API 中实现此功能,我还建议您在此处向 Google 提交增强票证。

于 2016-02-11T15:19:38.090 回答