我需要一个简单的 php IRC 机器人,它通过 POST 请求接收消息并将该消息发送到 irc 通道。为此,我改编了来自PHP-IRC Bot Not sent message Help的机器人。然后我使用消息队列将 POST 消息从IRC 相关帮助发送到机器人。
但是,当我通过 start.html 运行 php 脚本时,机器人甚至没有加入频道。irc.php ->
<?php
$ircServer = "irc.freenode.net";
$ircPort = "6667";
$ircChannel = "##my-channel";
set_time_limit(0);
$ircSocket = fsockopen($ircServer, $ircPort, $eN, $eS);
$msg = $_POST['msg'];
if ($ircSocket)
{
fwrite($ircSocket, "USER EDI Normandy-SR2 Alliance Dr-Eva\n");
fwrite($ircSocket, "NICK Hit-Hi-Fit-Hai\n");
fwrite($ircSocket, "JOIN " . $ircChannel . "\n");
fwrite($ircSocket, "PRIVMSG $ircChannel :$msg\n");
$queueKey = 123321;
$queue = false;
// Join the IPC queue
$queue = msg_get_queue($queueKey);
if(!$queue) echo "ERROR: Could not join IPC queue. Form data will not be received";
while(1)
{
while($data = fgets($ircSocket, 128))
{
echo nl2br($data);
flush();
$ex = explode(' ', $data);
if($ex[0] == "PING") fputs($socket, "PONG ".$ex[1]."\n");
if (msg_receive($queue, 0, $msgType, 1024, $msgData, true, MSG_IPC_NOWAIT)) {
//fwrite($ircSocket, "PRIVMSG $ircChannel :$msgData\n");
echo "callback working";
}
}
}
}
?>
我是这样称呼这个脚本的。开始.html ->
<html><body>
<h4>Start Bot</h4>
<form action="irc.php" method="post">
Command: <input type="text" name="msg" />
<input type="submit" />
</form>
</body></html>
如果我删除消息队列的代码,机器人会加入频道。