PHP socket_get_status() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
Network

Definition

In PHP, socket_get_status() returns metadata about an open stream, such as whether it is blocked, timed out, seekable, or at end-of-file.

It is an alias of stream_get_meta_data(). Both names call the same function. You can use it on sockets from fsockopen() or on ordinary streams such as fopen('php://temp'). The result is an associative array of status fields.

Syntax

Call socket_get_status() with the values you need. Start with this line.

PHP
$meta = socket_get_status($stream);
CallWhat it does
socket_get_status($stream)Returns the metadata array for the stream.
stream_get_meta_data($stream)Same call under the canonical stream name.
socket_get_status($stream)['timed_out']Reads whether the last read timed out.

Teach both names: socket_get_status() and stream_get_meta_data(). Pass any open stream resource. Common keys include timed_out, blocked, eof, wrapper_type, stream_type, mode, unread_bytes, seekable, and uri.

Library declarations
PHP
socket_get_status(resource $stream): array

Parameters

ParameterDescription
streamRequired. An open stream resource, for example from fopen(), fsockopen(), or pfsockopen().

Return Value

Return ValueDescription
arrayAssociative metadata about the stream's current status.

PHP Examples

Example 1 — Inspect a Temporary Stream

Open php://temp, write a little text, and print selected metadata keys with either alias name.

PHP
<?php
$stream = fopen('php://temp', 'r+');
fwrite($stream, 'hello');
rewind($stream);

$meta = socket_get_status($stream);
echo $meta['wrapper_type'], PHP_EOL;
echo $meta['stream_type'], PHP_EOL;
echo $meta['seekable'] ? 'seekable' : 'not seekable', PHP_EOL;

fclose($stream);

How It Works

  1. fopen('php://temp') creates an offline memory/temp stream.
  2. socket_get_status() is an alias of stream_get_meta_data().
  3. wrapper_type and stream_type describe the stream kind.
  4. seekable is true for php://temp, so rewind() works.

Example 2 — Compare Both Function Names

Call socket_get_status() and stream_get_meta_data() on the same handle and show they match.

PHP
<?php
$stream = fopen('php://temp', 'r+');
$a = socket_get_status($stream);
$b = stream_get_meta_data($stream);

echo ($a === $b) ? 'same metadata' : 'different', PHP_EOL;
echo 'blocked: ', $a['blocked'] ? 'yes' : 'no', PHP_EOL;
echo 'eof: ', $a['eof'] ? 'yes' : 'no', PHP_EOL;
fclose($stream);

How It Works

  1. Both functions return the same array for the same stream.
  2. A fresh temp stream is usually blocked by default.
  3. eof is false until you read past the end.
  4. Strict === confirms the alias relationship.

Example 3 — Read unread_bytes After Writing

Write data without rewinding and check unread_bytes and uri from the metadata array.

PHP
<?php
$stream = fopen('php://temp', 'r+');
fwrite($stream, 'abc');
$meta = stream_get_meta_data($stream);

echo 'uri: ', $meta['uri'], PHP_EOL;
echo 'mode: ', $meta['mode'], PHP_EOL;
echo 'unread_bytes: ', $meta['unread_bytes'], PHP_EOL;
fclose($stream);

How It Works

  1. uri shows the stream path php://temp.
  2. mode echoes the fopen mode r+.
  3. unread_bytes is about buffered incoming data; here it is 0.
  4. Using the stream_* name in this example reminds you both names are valid.

Frequently Asked Questions

No. socket_get_status() is an alias of stream_get_meta_data(). They do the same work.
Status fields such as timed_out, blocked, eof, wrapper_type, stream_type, mode, unread_bytes, seekable, and uri.
Yes. Any open stream works, which makes offline demos easy without a real network socket.
stream_get_meta_data() is the clearer modern name, but you will still see socket_get_status() in older network examples.
After a timed-out read, check socket_get_status($stream)['timed_out'] or the stream_get_meta_data() equivalent.

Next: socket_set_blocking()

socket_set_blocking() switches a stream between blocking mode and non-blocking mode.

socket_set_blocking() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful