Reputation: 31
Hello i've tried to connect to the Twitch IRC Chat so i can try to make a simple chat bot for twitch but im struggling to make it work.
Error im getting: http://puu.sh/j3HwK/173a0388fb.png
and here is the code:
<?php
set_time_limit(0);
ini_set('display_errors', 'on');
function IRCBot()
{
function IRC()
{
$config = array(
'server' => 'irc.twitch.tv',
'port' => 6667,
'channel' => '#spiritboar',
'name' => 'bin4rybot',
'nick' => 'Bin4ryBOT',
'pass' => 'oauth:##########################' //http://twitchapps.com/tmi/
);
echo 'test';
$server = array();
$server['connect'] = fsockopen($config['server'], $config['port']);
if($server['connect'])
{
echo 'test2';
SendData("PASS " . $config['pass'] . "\n\r");
SendData("NICK " . $config['nick'] . "\n\r");
SendData("USER " . $config['nick'] . "\n\r");
SendData("JOIN " . $config['channel'] . "\n\r");
while(!feof($server['connect']))
{
echo 'test3';
}
}
}
function SendData($cmd)
{
global $server;
fwrite($server['connect'], $cmd, strlen($cmd));
echo "[SEND] $cmd <br>";
}
IRC();
}
IRCBot();
?>
So basicly i cant make it connect to the Twitch IRC, Please if someone can help me it would be much appreciated! :)
Upvotes: 3
Views: 3388
Reputation: 91
I know it was asked soo manny years ago, but perhaps can help someone else today:
<?php
set_time_limit(0);
ini_set('display_errors', 'on');
$server = array();
function IRCBot()
{
function IRC()
{
$server global;
$config = array(
'server' => 'ssl://irc.chat.twitch.tv',
'port' => 6697,
'channel' => '#twitch_channel',
'nick' => strtolower('twitch_username'),
'pass' => 'oauth:twitch_oauth_token' //http://twitchapps.com/tmi/
);
$server['connect'] = @fsockopen($config['server'], $config['port']);
if($server['connect'])
{
echo "[<] Starting connection with user: " . $config['nick'];
SendData('CAP REQ :twitch.tv/tags');
SendData('CAP REQ :twitch.tv/commands');
SendData('CAP REQ :twitch.tv/membership');
SendData("PASS " . $config['pass']);
SendData("NICK " . $config['nick']);
SendData("USER " . $config['nick'] . " 1 1 :" . $config['nick']);
SendData("JOIN " . $config['channel']);
while(!feof($server['connect']))
{
$server['READ_BUFFER'] = fgets($server['connect'], 1024);
echo "[>] " . $server['READ_BUFFER'];
flush();
}
}
}
function SendData($cmd)
{
global $server;
@fwrite($server['connect'], $cmd . "\r\n");
echo "[<] $cmd \r\n";
}
IRC();
}
IRCBot();
The most important thing to notice beyond the format of the commands send to twitch, it's that the SendData function does not send strlen on the fwrite function and the "\r\n" append to command string.
Upvotes: 2
Reputation: 1808
In your IRC()
function, you've declared $server
, but not marked it as a global variable, thus it's not available in SendData(...)
.
SendData(...)
then looks at the global version of $server
, finds that it's not got an array element called connect
, so returns null, which is where your error is coming from.
Add a global $server;
before $server = array();
, and try again.
As a second point, I think the line returns need to be \r\n
instead of \n\r
, but I'm not sure - it will depend on how strict the IRC server is.
Upvotes: 0