Help with fsockopen

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Cirdan
Forum Contributor
Posts: 144
Joined: Sat Nov 01, 2008 3:20 pm

Help with fsockopen

Post by Cirdan »

Never mind...it was because I was returning 0 at the end...which is equal to false :P.

I am using MAMP, and am trying to use fsockopen to connect to an IRC server to receive the number of users in the channel. But I am getting error number 0, with no error string. No PHP errors.

hostname = 76.107.162.100
port = 11111

Code: Select all

    // Returns the number of users in the IRC
    // Returns false if failed
    private function _getIrcCount()
    {
        
        $socket = fsockopen($this->_hostname, $this->_ircPort, $error_num, $error_str, 30);
        
        echo $error_num.' '.$error_str;
        if(!$socket)
            return false;
            
        stream_set_timeout($socket, 1);
        fwrite($socket, "nick robot\nuser b b b b\nnames #redeemed\nquit\n");
        
        $buffer = "";
        
        while(!feof($socket))
            $buffer .= fgets($socket, 16);
            
        fclose($socket);
        
        if (preg_match ('/= #redeemed <!-- s:( --><img src=\"{SMILIES_PATH}/icon_sad.gif\" alt=\":(\" title=\"Sad\" /><!-- s:( -->.*)\s*\n/', $buffer, $matches))
        {
            $output = $matches[1];
            $output = preg_replace("/\s+$/", "", $output);
        }
        else
        {
            $output = 0;
        }
        
        return $output;
    }
Post Reply