以反向时间顺序获取聊天消息

时间:2016-02-11 13:32:01

标签: youtube-api youtube-livestreaming-api

我想从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);
    }

1 个答案:

答案 0 :(得分:0)

似乎没有一个参数可以传递到您的liveChatMessages/list端点请求中,以便首先返回最新消息。

您必须下载所有邮件,并按照每个聊天邮件snippet.publishedAt下的ISO 8601格式化日期对其进行排序。

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