User Tools

Site Tools


uphp:functions:fsockopen

This is an old revision of the document!


fsockopen

WMPRO, WMMINI FW >= 1.0 WMMEGA FW >= 2.0

Open an internet socket connection with optional timeout

Description

int fsockopen ( string $host, int $port [, int $timeout ] )

Attempts to create a socket to a remote host using the protocol specified, and return the file handle for future operations. 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==== <html><span style=“color:blue;”><b>host</b><span style=“color:black;”></html>: Host name or IP address of server to connect to <html><span style=“color:blue;”><b>port</b><span style=“color:black;”></html>: Port number <html><span style=“color:blue;”><b>timeout</b><span style=“color:black;”></html>: Connection timeout in seconds, optional (if not specified the system wide setting in [os:config:uphp.ini]] will be used). ====Return Value==== Upon success it returns a file handle which may be used together with the other file functions that require a handle such as fgets(), fwrite(), fclose() and feof() If the attempt to open the connection fails it will return 0 ====Example==== Create a connection to www.example.com and display the response: <code php> <? $fp=fsockopen(“www.example.com”,80,30); if (!$fp) { print(“Socket open failed”); } 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); } ?> </code> ====See Also==== fseropen() - Open the serial port at the specified baud rate with optional parameters f485open() - Open the RS-485 port as a stream fopen() - Open a file for reading or writing fclose() - Close a file, stream or socket feof() - Test if no more data is available in a file or stream fgets() - Return a single line from a file or stream, with optional size limit fread() - Return bytes from a file or stream fwrite() - Write data to a file or stream

uphp/functions/fsockopen.1489865318.txt.gz · Last modified: 2021/09/13 05:56 (external edit)