English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
The fsockopen() function opens a network connection or a Unix socket connection.
resource fsockopen ( string $hostname [, int $port = -1 [, int &$errno [, string &$errstr [, float $timeout = ini_get("default_socket_timeout") ]]] )
It is used to open Internet or Unix domain socket connections.
Initialize a socket connection to a specified host (hostname).
PHP supports the following list of socket transport types: The list of supported socket transports (Socket Transports). You can also obtain the supported types of socket transports by using stream_get_transports().
By default, the socket connection will be opened in blocking mode. Of course, you can change it to non-blocking mode by stream_set_blocking().
stream_socket_client() is very similar and provides more rich parameter settings, including non-blocking mode and context settings.
fsockopen() returns a file handle that can be used by other file class functions (such as fgets(), fgetss(), fwrite(), fclose(), and feof()). If the call fails, it will return FALSE.
Note: If the hostname is inaccessible, a warning-level (E_WARNING) error message will be thrown.
Number | Parameters and descriptions |
---|---|
1 | hostname If OpenSSL is installed, you may need to add the access protocol ssl: in front of your hostname address.//or tls://, thus it can use TCP/The SSL or TLS client of the IP protocol connects to the remote host. |
2 | port Port number. If you pass a parameter to this-1If the port is not specified, it means that the port is not used, for example, unix://. |
3 | errno Save the system-level error number that occurs in the system-level connect() call. |
4 | errstr Error information will be returned as a string. |
5 | timeout Set the timeout for the connection, in seconds. |
Try the following example
<?php $connection = fsockopen("www.w"3codebox.com", 80, $errno, $errstr, 30); if (!$connection) { echo "$errstr ($errno" \n" } else { $out = "GET" / HTTP/1.1\r\n" $out .= "Host: www.w"3codebox.com\r\n"; $out .= "Connection: Close\r\n\r\n"; fwrite($connection, $out); while (!feof($connection)) { echo fgets($connection, 128); } fclose($connection); } ?>
The example above opens a connection