fsockopen

fsockopen -- Open Internet or Unix domain socket connection.

Description

int fsockopen(string hostname, int port, int [errno], string [errstr]);

Opens an Internet domain socket connection to hostname on port port and returns a file pointer, which may be used by fgets(), fgetss(), fputs(), and fclose(). If the call fails, it will return FALSE and if the optional errno and errstr arguments are present they will be set to indicate the actual system level error that ocurred on the system-level connect() call. If the returned errno is 0, but the function returned FALSE, it is an indication that the error occurred before the connect() call. This is most likely due to a problem initializing the socket. Note that the errno and errstr arguments should be passed by reference.

If port is 0 and the operating system supports Unix domain sockets, hostname will be used as the filename of a Unix domain socket to connect to.

The socket will by default be opened in blocking mode. You can switch it to non-blocking mode by using the set_socket_blocking().

Example 1. fsockopen example

$fp = fsockopen("www.php.net", 80, &$errno, &$errstr);
if(!$fp) {
	echo "$errstr ($errno)<br>\n";
} else {
	fputs($fp,"GET / HTTP/1.0\n\n");
	while(!feof($fp)) {
		echo fgets($fp,128);
	}
	fclose($fp);
}