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 or stream, with optional size limit

Description

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

This function works with different resource types. If no size is specified, it will keep reading from the file or stream until it reaches the end of the line (or the end of the file). For sockets and streams 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
  • -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

fread() - Return a specified number of bytes from a file or stream

fwrite() - Write data to a file or stream

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

fopen() - Open a file for reading or writing

f485open() - Open the RS-485 port as a stream

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

fclose() - Close a file, stream or socket

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