好的。我知道這個問題,我會盡力去描述它,但是我不能爲了我的生活找到答案,回答這個問題。PHP IRC Bot - 等待001
好吧,我的機器人會連接到服務器。但這個特定的服務器,你必須等待:
:vervet.foonetic.net 001 Eve :Welcome to the Foonetic IRC Network [email protected]*-*-*-*.*.*.*.net<br />
:vervet.foonetic.net 002 Eve :Your host is vervet.foonetic.net, running version Unreal3.2.8.1<br />
:vervet.foonetic.net 003 Eve :This server was created Thu Feb 24 2011 at 00:34:28 EST<br />
注意第一行。 「001」。 我的機器人需要等待該線顯示出來,然後才能運行JOIN命令。它不會D: 我嘗試了一段時間(){}之前while(1){},我嘗試了一個do {} while()和一個簡單的if()。我不是最好的 PHP scripter所以,裸露在我身邊。但是,是的,任何幫助表示讚賞。
這是我的代碼。
<?php
set_time_limit(0);
$conf = array('server' => 'irc.foonetic.net', 'port' => '6667', 'channel' => '#lingubender', 'nick' => 'Eve', 'name' => 'EveBot_1.0', 'description' => 'An IRC bot created by PHP scripter Kinz');
$replies = array('ver' => 'Eve 1.0', 'part' => 'Good-bye!', 'quit' => 'Shutting down. Talk to you all later!', 'regUsername' => '[email protected]', 'regPassword' => 'Example');
$socket = fsockopen($conf['server'], $conf['port']);
fputs($socket, "USER ".$conf['nick'].' '.$conf['description'].' '.$conf['nick'].' :'.$conf['name']."\n");
fputs($socket, "NICK ".$conf['nick']."\n");
fputs($socket, "JOIN ".$conf['channel']."\n");
while(1) {
while($data = fgets($socket, 128)) {
echo nl2br($data);
flush();
$grip = explode(' ', $data);
if ($grip[0] == "PING") {
fputs($socket, "PONG ".$grip[1]."\n");
}
$chan = $grip[2];
$command = str_replace(array(chr(10), chr(13)), '', $grip[3]);
strtolower($command);
if ($command == ":eve" || $command == ":eve,") {
fputs($socket, "PRIVMSG ".$chan." :Name recognition acknowledged.\n");
$ex = $grip[4];
strtolower($ex);
switch($ex) {
case "shutdown":
fputs($socket, "QUIT ".$replies['quit']."\n");
break;
case "version":
fputs($socket, "PRIVMSG ".$chan[2]." :".$replies['ver'].' : '.$conf['description']." \n");
break;
}
} else {
NULL;
}
}
}
?>
你可以用'fgets'做一個while循環,直到你在連接之後從套接字中獲得你需要的數據。 – Yaniro 2012-03-01 10:35:53