PHP socket connection to Twitch IRC server not working

I’m trying to build a bot for Twitch chat using PHP but running into connection issues with their IRC server.

I’ve been working on this for a while and can’t figure out what’s going wrong. The connection attempt fails and I’m not sure if it’s my code structure or something with the authentication.

Here’s what I have so far:

<?php

set_time_limit(0);
error_reporting(E_ALL);

class TwitchChatBot {
    private $socket;
    private $settings = [
        'host' => 'irc.twitch.tv',
        'port' => 6667,
        'room' => '#testchannel',
        'username' => 'mychatbot',
        'nickname' => 'MyChatBot',
        'token' => 'oauth:your_token_here'
    ];
    
    public function startBot() {
        $this->connectToIRC();
    }
    
    private function connectToIRC() {
        echo 'Attempting connection...';
        $this->socket = fsockopen($this->settings['host'], $this->settings['port']);
        
        if ($this->socket) {
            echo 'Connected successfully';
            $this->authenticate();
            $this->joinChannel();
            $this->listenForMessages();
        }
    }
    
    private function authenticate() {
        $this->sendCommand("PASS " . $this->settings['token']);
        $this->sendCommand("NICK " . $this->settings['nickname']);
        $this->sendCommand("USER " . $this->settings['username']);
    }
    
    private function joinChannel() {
        $this->sendCommand("JOIN " . $this->settings['room']);
    }
    
    private function listenForMessages() {
        while (!feof($this->socket)) {
            // Listen for incoming data
        }
    }
    
    private function sendCommand($message) {
        fwrite($this->socket, $message . "\r\n");
        echo "[SENT] $message\n";
    }
}

$bot = new TwitchChatBot();
$bot->startBot();

?>

The connection fails right at the fsockopen part. Has anyone successfully connected to Twitch IRC using PHP? What am I missing here?

check if your server allows outbound connections on port 6667. Many hosting providers block IRC ports by default. add some error checking after fsockopen: if (!$this->socket) { echo 'Connection failed: ' . socket_strerror(socket_last_error()); } this’ll show you what’s actually going wrong.

You need delays between your authentication commands. Twitch IRC can’t handle rapid-fire requests - add usleep(500000) after each sendCommand in your authenticate method. Your fsockopen is missing error handling. Use fsockopen($host, $port, $errno, $errstr, 30) to catch connection failures. Token format looks right, but make sure it’s actually a valid OAuth token from Twitch’s API, not just a placeholder. Your listenForMessages method is empty - that’s a problem. You need to read from the socket with fgets() and handle PING responses or Twitch will boot you after a few minutes.

Had the same problem when I started with Twitch IRC. fsockopen doesn’t play nice with SSL, and Twitch wants secure connections now. Switch to stream_socket_client with SSL context instead. Double-check your OAuth token has the right format and scopes - you need chat:read and chat:edit minimum. Your USER command syntax might be off too - should be USER username 8 * :Real name with those extra parameters. Also throw in some error handling with stream_get_meta_data to catch connection timeouts.