PHP 提取 youtube 实时聊天 [英] PHP Extract youtube live chat
问题描述
当在 youtube 上进行直播时,视频播放器的右侧总是会出现聊天.我正在尝试将聊天中的消息下载到我的 PHP 页面,以便过滤它们,因为它们滚动得非常快,我无法赶上它们.
When there's live broadcast on youtube there's always a chat to the right of video player. I am trying to download messages from chat to my PHP page in order to filter trough them as they scroll really fast and i cannot catch up with them.
我注意到,当您通过实时聊天打开 youtube 页面时,它每 10 秒运行一次到此 URL 的 GET
I noticed that when you open youtube page with live chat it runs GET to this URL every 10 seconds
https://www.youtube.com/live_comments?action_get_comments=1&video_id=<video_id><=<timestamp of last recorded message>&format=proto&pd=10000&rc=26&scr=true&comment_version=1
如果我这样做
$xml= file_get_contents('https://www.youtube.com/live_comments?action_get_comments=1&video_id=<video_id><=<timestamp of last recorded message>&format=proto&pd=10000&rc=26&scr=true&comment_version=1');
echo "<textarea>{$xml}</textarea>";
我得到了关注
<?xml version="1.0" encoding="utf-8"?>
<root>
<latest_time>
<![CDATA[1445398692]]>
</latest_time>
<return_code>
<![CDATA[0]]>
</return_code>
<likes_data>
<![CDATA[{"percent_dislikes":7,"num_likes":"1,383","num_dislikes":"91","percent_likes":93}]]>
</likes_data>
<comments>
<![CDATA[EOcKGFsgkE4=]]>
</comments>
</root>
我认为这会让我出现新消息,但我目前正在测试没有人的聊天,我无法确认.
I assume this would get me new messages that appear however i am currently testing on chat that has no people in it and i cannot confirm.
每 10 秒也有一次 POST 请求,但我并不是要发布只是下载.
There's also POST request every 10 seconds however i am not trying to post just download.
我的问题是,当您最初打开页面时,如何使用 PHP 下载聊天中显示的现有消息?
My question is how do i download with PHP existing messages that show in the chat when you open page initially?
推荐答案
这不是一个足够的答案(没有 PHP,没有代码...),但是如果你对一个肮脏而简单的解决方案感兴趣(抓住一个 Youtube Live视频聊天为 JSON),您可以使用浏览器执行此操作.在 Firefox 中:
This is not an adequate answer (no PHP, no code...), but if you are interested in a dirty and simple solution (grabbing a Youtube Live video Chat as JSON), you can use your browser to do so. In Firefox:
- 在 Firefox 浏览器中的视频聊天窗口顶部加载视频网址.
- 选择实时聊天重播"以获取所有聊天消息.
- 暂停视频.
- 在 FF 菜单上,依次转到Web 开发者工具"和网络".
- 标记永久寄存器"复选框(可选)
- 仅选择XHR"并按live_chat_replay"过滤结果.
- 播放视频并让它连续播放...
- 最后,再次暂停视频,在网络"控制台的右上角找到并点击HAR"选择器.
- 选择全部另存为 HAR"
- 选择文件目的地并保存.
- 在文本/代码编辑器中打开HAR"文件(它是 JSONS 的 JSON)
- 使用您最喜欢的语言来解析它.
- 干杯!
这篇关于PHP 提取 youtube 实时聊天的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!