Skip to content

Instantly share code, notes, and snippets.

@nico-lab
Last active August 29, 2015 14:20
Show Gist options
  • Save nico-lab/7a17415f118cd48545b4 to your computer and use it in GitHub Desktop.
Save nico-lab/7a17415f118cd48545b4 to your computer and use it in GitHub Desktop.
ニコ生アラートコメントサーバ接続する
<?php
// みゆっき氏に感謝
// https://gist.github.com/miyukki/2520787
/* ニコ生アラートコメントサーバ接続先一覧
twr01.live.nicovideo.jp:2525/1000000000
twr01.live.nicovideo.jp:2526/1000000002
twr01.live.nicovideo.jp:2527/1000000004
twr01.live.nicovideo.jp:2528/1000000006
twr01.live.nicovideo.jp:2529/1000000008
twr01.live.nicovideo.jp:2530/1000000010
twr01.live.nicovideo.jp:2531/1000000012
twr01.live.nicovideo.jp:2532/1000000014
twr01.live.nicovideo.jp:2533/1000000016
twr01.live.nicovideo.jp:2534/1000000018
twr02.live.nicovideo.jp:2525/1000000001
twr02.live.nicovideo.jp:2526/1000000003
twr02.live.nicovideo.jp:2527/1000000005
twr02.live.nicovideo.jp:2528/1000000007
twr02.live.nicovideo.jp:2529/1000000009
twr02.live.nicovideo.jp:2530/1000000011
twr02.live.nicovideo.jp:2531/1000000013
twr02.live.nicovideo.jp:2532/1000000015
twr02.live.nicovideo.jp:2533/1000000017
twr02.live.nicovideo.jp:2534/1000000019
*/
// ニコ生アラートコメントサーバに接続してアラートを取得
$msg_socket = fsockopen("twr01.live.nicovideo.jp", 2530);
stream_set_blocking($msg_socket, 0);
fputs($msg_socket, sprintf('<thread thread="1000000010" version="20061206" res_from="-1" />'."\0"));
// コメントをループして出力
$buffer = "";
while(1) {
$buffer .= fread($msg_socket, 1024);
$commentbuffer = explode("\0", $buffer, 2);
if(isset($commentbuffer[1])) {
$buffer = $commentbuffer[1];
$comment = new SimpleXMLElement($commentbuffer[0]);
echo $comment['no'] . ' - ' . $comment .PHP_EOL;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment