stream_get_meta_data

(PHP 4 >= 4.3.0, PHP 5)

stream_get_meta_data -- Retrieves header/meta data from streams/file pointers

Description

array stream_get_meta_data ( resource stream )

Returns information about an existing stream. The stream can be any stream created by fopen(), fsockopen() and pfsockopen(). The result array contains the following items:

  • timed_out (bool) - TRUE if the stream timed out while waiting for data on the last call to fread() or fgets().

  • blocked (bool) - TRUE if the stream is in blocking IO mode. See stream_set_blocking().

  • eof (bool) - TRUE if the stream has reached end-of-file. Note that for socket streams this member can be TRUE even when unread_bytes is non-zero. To determine if there is more data to be read, use feof() instead of reading this item.

  • unread_bytes (int) - the number of bytes currently contained in the PHP's own internal buffer.

    Замечание: You shouldn't use this value in a script.

The following items were added in PHP 4.3:

  • stream_type (string) - a label describing the underlying implementation of the stream.

  • wrapper_type (string) - a label describing the protocol wrapper implementation layered over the stream. See Прил. M for more information about wrappers.

  • wrapper_data (mixed) - wrapper specific data attached to this stream. See Прил. M for more information about wrappers and their wrapper data.

  • filters (array) - and array containing the names of any filters that have been stacked onto this stream. Documentation on filters can be found in the Filters appendix.

Замечание: This function was introduced in PHP 4.3, but prior to this version, socket_get_status() could be used to retrieve the first four items, for socket based streams only.

In PHP 4.3 and later, socket_get_status() is an alias for this function.

Замечание: This function does NOT work on sockets created by the Socket extension.

The following items were added in PHP 5.0:

  • mode (string) - the type of access required for this stream (see Table 1 of the fopen() reference)

  • seekable (bool) - whether the current stream can be seeked.

  • uri (string) - the URI/filename associated with this stream.



stream_get_meta_data
ed at readinged dot com
28-Jan-2003 07:54
Below is a function I wrote to pull the "Last-Modified" header from a given URL.  In PHP version 4.3 and above, it takes advantage of the stream_get_meta_data function, and in older version it uses a conventional GET procedure.  On failure to connect to $url, it returns NULL.  If the server does not return the Last-Modified header, it returns the current time.  All times are returned in PHP's integer format (seconds since epoch).

Use it as so:

$last_modified = stream_last_modified('news.rss');
if (!is_null($last_modified))
   if ($last_modified < time()-3600) //Older than an hour
     echo 'URL is older than an hour.';
   else
     echo 'URL is fairly new.';
else
   echo 'Invalid URL!';

function stream_last_modified($url)
{
   if (function_exists('version_compare') && version_compare(phpversion(), '4.3.0') > 0)
   {
     if (!($fp = @fopen($url, 'r')))
         return NULL;

     $meta = stream_get_meta_data($fp);
     for ($j = 0; isset($meta['wrapper_data'][$j]); $j++)
     {
         if (strstr(strtolower($meta['wrapper_data'][$j]), 'last-modified'))
         {
           $modtime = substr($meta['wrapper_data'][$j], 15);
           break;
         }
     }
     fclose($fp);
   }
   else
   {
     $parts = parse_url($url);
     $host  = $parts['host'];
     $path  = $parts['path'];

     if (!($fp = @fsockopen($host, 80)))
         return NULL;

     $req = "HEAD $path HTTP/1.0\r\nUser-Agent: PHP/".phpversion()."\r\nHost: $host:80\r\nAccept: */*\r\n\r\n";
     fputs($fp, $req);

     while (!feof($fp))
     {
         $str = fgets($fp, 4096);
         if (strstr(strtolower($str), 'last-modified'))
         {
           $modtime = substr($str, 15);
           break;
         }
     }
     fclose($fp);
  }
   return isset($modtime) ? strtotime($modtime) : time();
}

<stream_get_linestream_get_transports>
 Last updated: Tue, 15 Nov 2005