2014-09-01 159 views
0

我一直試圖通過IRC連接到Twitch聊天。我添加了一個回顯函數來測試我的連接與否,因爲該頁面是空白的,並且帳戶沒有加入IRC。fsocketopen()沒有連接到服務器?

這裏是我的代碼:

<?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 \n\r"); //Sends the password not needed for most servers 
      SendCommand("NICK $nickname\n\r"); //sends the nickname 
      SendCommand("USER $nickname USING PHP IRC\n\r"); //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>\n\r"; //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\n\r"); //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)."\n\r"); //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 
} 
?> 

看來我不能獲得通過if($server['SOCKET'])。無論如何,我可以診斷這個?因爲我用hexChat直接與細節連接。

回答

1

服務器實際上一直阻止我使用fsocket。聯繫我的主機並離開共享主機後,它開始工作。

0

請看看這個答案:https://stackoverflow.com/a/24835967/1163786

特別:

$socket = fsockopen($bot["Host"], $bot["Port"], $error1, $error2); 
if(!$socket) { 
    echo 'Crap! fsockopen failed. Details: ' . $error1 . ': ' . $error2; 
} 

要獲取有關套接字錯誤=原因插座不連接的細節。

目前,您在fsockopen上有$errno, $errstr,但只回顯「連接失敗」。

+0

嘿延斯,我一直在讀線程。它需要通過PHP來完成。 (除非有一種更簡單的方法來合併我用PHP編寫的腳本)。 – 2014-09-01 10:32:06