使用套接字创建聊天机器人无效

时间:2013-07-19 10:19:03

标签: php sockets

我正在尝试在xat.com(免费聊天)上制作聊天机器人。机器人使用套接字连接但不起作用。这是脚本:

<?php
$k1 = "-2129455030"; 
$k3 = "2091086330";    
$d0 = "1024";          
$d3 = "5368040";       
$dt = "1374147657";    
$id = "1309761795";    
$name = "iCloudv2Bot"; 
$avatar = "-1";       
$homepage = "Test";    
$roomID = "50998799";  
$user = "iCloudv2Bot"; 
$pass = "iCloudv2Bot"; 
$res = file_get_contents('http://arcbots.com/api.html&tools=chatip&chatroom='.$roomID);
$data = json_decode($res,true);
if($data['errorCode'] == 3) {
    $port = $data['port'];
    $ip = $data['ip'];
}
$socket = socket_create(AF_INET,SOCK_STREAM,SOL_TCP);
if ($socket == false) {
    echo "=> Error creating socket\n";
} else {
    echo "=> Socket successfully created\n";
}
$connect = socket_connect($socket, $ip, $port);
if ($connect == false) {
    echo "=> Error connecting to server\n";
} else {
    echo "=> Successfully connected to server\n";
}
    $ym = "<y m='1' />".chr(0);
    echo "=> Sent: ".$ym,"\n"; 
    $writepacket1 = socket_write($socket, $ym);
    $j2 = "<j2 q='1' y='822369830' k='".$k1."' k3='".$k3."' z='12' p='0' c='".$roomID."' f='0' u='".$id."' d0='0' n='".$name."' a='".$avatar."' h='".$homepage."' v='0' />".chr(0);
    echo "=> Sent: ", $j2, "\n"; 
    $writepacket2 = socket_write($socket, $j2);
    if($writepacket1 == false) { 
        echo "=> Error writing socket\n";
    } else {
        echo "=> Socket successfully written to server\n";
    }
    if ($writepacket2 == false) {
        echo "=> Error writing socket\n";
    } else {
        echo "=> Socket successfully written to server\n";
    }
     $socketread = socket_read($socket,  100000, PHP_BINARY_READ);
     echo "=> ", $socketread;
    if ($socketread){
     echo "=> Recieved information from server ", $socketread;
    } else {
     echo "=> Error reading socket\n";
    }  
    if (strpos($socketread, 'done')){
     echo "=> Successfully connected to xat\n";
    } else {
     echo "=> Error connecting to xat\n";
    }  
?>

当我运行代码时,它在部分结束:

if ($writepacket2 == false) {
    echo "=> Error writing socket\n";
} else {
    echo "=> Socket successfully written to server\n";
}

在该部分之后没有任何事情发生,它应该读取数据包并连接,但事实并非如此。任何人都可以帮助我,或者有人知道什么是错的吗?三江源!

0 个答案:

没有答案