您好,我已经尝试连接到Twitch IRC聊天室,所以我可以尝试制作一个用于抽搐的简单聊天机器人,但我一直在努力使其工作。

我收到错误消息:http://puu.sh/j3HwK/173a0388fb.png

这是代码:

<?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();

?>

因此,基本上我无法使其连接到Twitch IRC,如果有人可以帮助我,将不胜感激! :)

最佳答案

我知道几年前有人问过这个问题,但今天也许可以帮助其他人:

<?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();

除了发送到twitch的命令格式以外,需要注意的最重要的事情是,SendData函数不会在fwrite函数上发送strlen ,并且“\r\n” 会附加到命令字符串。

关于php - 连接到Twitch IRC聊天室,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/31491759/

10-09 00:58