User Tools

Site Tools


uphp:functions:fsockopen

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

$host: Host name or IP address of server to connect to

$port: Port number

$timeout: Connection timeout in seconds, optional (if not specified the system wide setting in uphp.ini will be used).

Return Values

Integer: File handle, or 0 if the attempt to open the connection fails

The file handle may be used together with the other file functions that require a handle such as fgets(), fwrite(), fclose() and feof()

Example

Create a connection to www.example.com and display the response

<?
  $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);
  }
?>

Also See

fopen() - Open a file for reading or writing

fseropen() - Open the serial port at the specified baud rate with optional parameters

f485open() - Open the RS-485 port at the specified baud rate and parity

fread() - Read bytes from a file, stream or socket

fgets() - Return a single line from a file, stream or socket, with optional size limit

fwrite() - Write data to a file, stream or socket

feof() - Test if no more data is available in a file, stream or socket

filesize() - Return the size of a file, or the number of unread bytes in a stream or socket

fseek() - Position the file pointer in an open file

ftell() - Return the current position of a file read/write pointer

fclose() - Close a file, stream or socket

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