I'm running a Node.js WebSocket server on port 3000 on the same machine that I'm running Apache/PHP by running a reverse-proxy through Apache. This is my HTTPD file:
<VirtualHost *:80>
ProxyRequests off
SSLProxyEngine on
ProxyPass /node/ ws://localhost:3000/node/
ProxyPassReverse /node/ ws://localhost:3000/node/
</VirtualHost>
<VirtualHost *:443>
ProxyRequests off
SSLProxyEngine on
ProxyPass /node/ ws://localhost:3000/node/
ProxyPassReverse /node/ ws://localhost:3000/node/
</VirtualHost>
Let's say my domain name is "example.com". Then, to connect to the WebSocket server through a browser, I would use this JS:
var connection = new WebSocket("wss://example.com/node/");
This works fine, and I'm successfully sending and receiving messages from browsers.
However, I also need to be able to send (not receive) messages to the WebSocket server using PHP, from the same machine. I've tried several configurations of fsockopen, and none of them have worked. Here are the ones I've tried, with the resulting error messages:
$fp = fsockopen("example.com/node/", 3000); //php_network_getaddresses: getaddrinfo failed: Name or service not known
$fp = fsockopen("ssl://example.com/node/", 3000); //php_network_getaddresses: getaddrinfo failed: Name or service not known
$fp = fsockopen("tls://example.com/node/", 3000); //php_network_getaddresses: getaddrinfo failed: Name or service not known
$fp = fsockopen("http://example.com/node/", 3000); //Unable to find the socket transport "http" - did you forget to enable it when you configured PHP?
$fp = fsockopen("https://example.com/node/", 3000); //Unable to find the socket transport "https" - did you forget to enable it when you configured PHP?
$fp = fsockopen("wss://example.com/node/", 3000); //Unable to find the socket transport "wss" - did you forget to enable it when you configured PHP?
$fp = fsockopen("ws://example.com/node/", 3000); //Unable to find the socket transport "ws" - did you forget to enable it when you configured PHP?
$fp = fsockopen("example.com/node/", 443); //php_network_getaddresses: getaddrinfo failed: Name or service not known
$fp = fsockopen("ssl://example.com/node/", 443); //php_network_getaddresses: getaddrinfo failed: Name or service not known
$fp = fsockopen("tls://example.com/node/", 443); //php_network_getaddresses: getaddrinfo failed: Name or service not known
$fp = fsockopen("http://example.com/node/", 443); //Unable to find the socket transport "http" - did you forget to enable it when you configured PHP?
$fp = fsockopen("https://example.com/node/", 443); //Unable to find the socket transport "https" - did you forget to enable it when you configured PHP?
$fp = fsockopen("wss://example.com/node/", 443); //Unable to find the socket transport "wss" - did you forget to enable it when you configured PHP?
$fp = fsockopen("ws://example.com/node/", 443); //Unable to find the socket transport "ws" - did you forget to enable it when you configured PHP?
$fp = fsockopen("example.com/node/", 80); //php_network_getaddresses: getaddrinfo failed: Name or service not known
$fp = fsockopen("ssl://example.com/node/", 80); //php_network_getaddresses: getaddrinfo failed: Name or service not known
$fp = fsockopen("tls://example.com/node/", 80); //php_network_getaddresses: getaddrinfo failed: Name or service not known
$fp = fsockopen("http://example.com/node/", 80); //Unable to find the socket transport "http" - did you forget to enable it when you configured PHP?
$fp = fsockopen("https://example.com/node/", 80); //Unable to find the socket transport "https" - did you forget to enable it when you configured PHP?
$fp = fsockopen("wss://example.com/node/", 80); //Unable to find the socket transport "wss" - did you forget to enable it when you configured PHP?
$fp = fsockopen("ws://example.com/node/", 80); //Unable to find the socket transport "ws" - did you forget to enable it when you configured PHP?
I've also tried all of those configurations using "localhost" and "127.0.0.1". And I've also experimented using the built-in PHP library functions socket_create and socket_connect, with all of those same hostnames, IP's, and ports - all resulting in the same types of "unknown host" errors. What am I doing wrong? What is the right way to do this?