I need to connect to an external socket server via URL

Asked

Viewed 162 times

2

First of all, I know there are thousands of tutorials teaching sockets internet throughout. However, I did not find anything specifically for my difficulty, so I resorted to ONLY.

I need to create a service socket in PHP to connect as a client to a server socket external and listen to it. The problem is that host external is a url with parameters and not an IP. The methods known in PHP as socket_create() only allow receiving a HOST (IP) and not a URL.

The other known method stream_socket_server() allows connection to URL, but I could not remain "listening" to the server with it. All tutorials that I found also do not listen, runs only a script and closes the connection.

Anyway, I need some help opening a connection socket with a URL as a client and listening to it.

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);

1 answer

2


I solved my problem using the library ratchetphp/Pawl on Github. This library allows PHP connection as a client in a WSS.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.