User Tools

Site Tools


uphp:functions:fgets

This is an old revision of the document!


fgets

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

Return a single line from a file, stream or socket, with optional size limit

Description

mixed fgets ( int $handle [, int $size ] )

This function works with different resource types. If no size is specified, it will keep reading from the file, stream or socket until it reaches the end of the line (or the end of the file). For streams and sockets it will return characters until a line feed (LF) or carriage return (CR) is reached (it will wait until an entire line is received). If the optional size is specified and the line is longer than this size, the function will return only the number of characters specified.

Parameters

$handle: Valid handle of a previously opened resource

$size: Optional limit to the number of bytes returned

Return Values

  • string containing the line of characters (without CR/LF)
    • Empty string if there is no data on the line
  • int -1 for error

Example

Open a TCP connection to google.com and print the header for a search for the word 'test':

<pre><?
  $f=fsockopen("www.google.com",80,10);
  if ($f) {
    // send the request:
    $out = "GET /search?q=test HTTP/1.0\r\n";
    $out = $out."Host: www.google.com\r\n";
    $out = $out."Connection: Close\r\n\r\n";
    fwrite($f,$out);
    print($out);
    // wait for and receive the header (multiple lines):
    $done=0;
    while (!$done) {
      $str=fgets($f);
      print($str+"\r\n");
      if (strlen($str)==0) $done=1;
    }
    fclose($f);
  }
?></pre>

See Also

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

fsockopen() - Open an internet socket connection with optional timeout

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

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/fgets.1491401239.txt.gz · Last modified: 2021/09/13 05:56 (external edit)