dongqu7778 2014-09-01 09:08
浏览 41
已采纳

fsockopen()没有连接到服务器?

I've been trying to connect to a Twitch chat via IRC. I've added an echo function to test where I had connected or not as that page was blank and an account didn't join the IRC.

Here is my code:

<?php
    set_time_limit(0);
    ini_set('display_errors', 'on');

$datatwitch= array(
        'server' => 'irc.twitch.tv',
        'port' => 6667,
        'nick' => 'greatbritishbgbot',
        'name' => 'greatbritishbgbot',
        'pass' => 'oauth:HERE',
    );
    ?>
<?php 
//The server host is the IP or DNS of the IRC server. 
$server_host = $datatwitch['server'];  
//Server Port, this is the port that the irc server is running on. Deafult: 6667 
$server_port = $datatwitch['port'];  
//Server Chanel, After connecting to the IRC server this is the channel it will join. 
$server_chan = "#greatbritishbg"; 

//login password 
$nickname = $datatwitch['name']; 
$nickname_pass = $datatwitch['pass']; 

    //Ok, We have a nickname, now lets connect. 
    $server = array(); //we will use an array to store all the server data. 
    //Open the socket connection to the IRC server 
    $server['SOCKET'] = @fsockopen($server_host, $server_port, $errno, $errstr, 2); 
    if($server['SOCKET']) 
    { 
        //Ok, we have connected to the server, now we have to send the login commands. 
        echo "connected";
          SendCommand("PASS $nickname_pass 
"); //Sends the password not needed for most servers 
          SendCommand("NICK $nickname
"); //sends the nickname 
          SendCommand("USER $nickname USING PHP IRC
"); //sends the user must have 4 paramters 
        while(!feof($server['SOCKET'])) //while we are connected to the server 
        { 
            $server['READ_BUFFER'] = fgets($server['SOCKET'], 1024); //get a line of data from the server 
            echo "[RECIVE] ".$server['READ_BUFFER']."<br>
"; //display the recived data from the server 

            /* 
            IRC Sends a "PING" command to the client which must be anwsered with a "PONG" 
            Or the client gets Disconnected 
            */ 
            //Now lets check to see if we have joined the server 
            if(strpos($server['READ_BUFFER'], "422")) //422 is the message number of the MOTD for the server (The last thing displayed after a successful connection) 
            { 
                //If we have joined the server 

                SendCommand("JOIN $server_chan
"); //Join the chanel 
            } 
            if(substr($server['READ_BUFFER'], 0, 6) == "PING :") //If the server has sent the ping command 
            { 
                SendCommand("PONG :".substr($server['READ_BUFFER'], 6)."
"); //Reply with pong 
                //As you can see i dont have it reply with just "PONG" 
                //It sends PONG and the data recived after the "PING" text on that recived line 
                //Reason being is some irc servers have a "No Spoof" feature that sends a key after the PING 
                //Command that must be replied with PONG and the same key sent. 
            } 
            flush(); //This flushes the output buffer forcing the text in the while loop to be displayed "On demand" 
        } 
    } else {
        echo "connection failed";
    }
function SendCommand ($cmd) 
{ 
    global $server; //Extends our $server array to this function 
    @fwrite($server['SOCKET'], $cmd, strlen($cmd)); //sends the command to the server 
    echo "[SEND] $cmd <br>"; //displays it on the screen 
} 
?>

It appears I can't Get passed if($server['SOCKET']). Is there anyway I can diagnose this? As I have directly connect with the details in hexChat.

  • 写回答

2条回答 默认 最新

  • doz95923 2014-09-05 11:15
    关注

    The server had actually been preventing me to use fsocket. After contacting my host and moving away from a shared host it started to work.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题