fsockopen

(FW >= 1.0)

fsockopen - Open a socket to a remote host

Description

int fsockopen( string $hostname, int $port [, int $timeout ] )
Initiates a socket connection to the resource specified by hostname. By default a TCP/IP connection will be initiated.  If a UDP connection is required prefix the host name with udp:// (lower case).

The socket will be opened in blocking mode.

Parameters

hostname

Host name or IP address of server to connect to.

port

The port number.

timeout

The connection timeout, in seconds. If not specified, this will use the system wide setting in uphp.ini

Return Value

fsockopen() returns a file index which may be used together with the other file functions (such as fgets(), fwrite(), fclose(), and feof()). If the call fails, it will return 0.

Examples

<? 
$fp = fsockopen("www.example.com", 80, 30);
if (!$fp) {
    print("Socket open failed\n");
} else {
    $out = "GET / HTTP/1.1\r\n";
    $out .= "Host: www.example.com\r\n";
    $out .= "Connection: Close\r\n\r\n";
    fwrite($fp, $out);
    while (!feof($fp)) {
        echo(fgets($fp, 128));
    }
    fclose($fp);
}
?>