phpsocketswebsocketfsockopen

Connect as client in a remote socket server


I need to build a simple PHP service socket to connect as client with a remote socket server and lintening then. The problem is that I need to connect with the host URL and not the host IP.

I've tried with socket_create() method, but it only connect using IP. Other method that I've tried was stream_socket_server() that allow URL conections, but I cannot keep up a listening to receive responses.

echo "Socket started...\n";

$server = 'ssl://sockserver.com/moreurl/params?56298';
$host = "sockserver.com";
$port = 80;
set_time_limit(0);

$socket = socket_create(AF_INET, SOCK_STREAM, 0);
$result = socket_connect($socket, $server, $port);
socket_bind($socket, $host);
socket_listen($socket);

while (true) {
  $result = socket_read ($socket, 1024);
  if($result){
    echo "Reply From Server  :".$result;
  }
}

socket_close($socket);

So, I need to keep a listening to a remote server Socket as PHP client using an URL like host.


Solution

  • I solved my problem using the ratchetphp/Pawl library at Github. This library allow to connect as client in a WSS.