Use fsockopen() when you need to opens a TCP or UDP client socket using PHP stream APIs in a PHP application. The examples below show the actual operation and its limitations.
What Is fsockopen() in PHP?
Opens a TCP or UDP client socket using PHP stream APIs. The exact signature supported by your installation depends on its PHP version and enabled extensions.
Its syntax is:
fsockopen(string $hostname, int $port = ..., &$error_code = ..., &$error_message = ..., ?float $timeout = ...)
Parameters and return value: The parameter types, optional arguments, references, and return type are shown in the signature above. Optional parameters are marked with an ellipsis in this reference signature.
Example 1: Basic Usage
<?php
$socket = @fsockopen("127.0.0.1", 1, $errorNumber, $errorMessage, 0.2); echo $socket ? "Connected" : "Unavailable"; if ($socket) fclose($socket);
Result: Run the example in an environment that supports the function. Any values depending on operating-system state, installed extensions, process configuration, or network access must be checked on the target server.
Example 2: Different Input or Behavior
<?php
$socket = @fsockopen("127.0.0.1", 9, $errorNumber, $errorMessage, 0.2); var_dump(is_resource($socket)); if ($socket) fclose($socket);
Result: Run the example in an environment that supports the function. Any values depending on operating-system state, installed extensions, process configuration, or network access must be checked on the target server.
Practical Example: Applying the Function
<?php
$host = "127.0.0.1"; $port = 9; $socket = @fsockopen($host,$port,$errorNumber,$errorMessage,0.2); if ($socket === false) { echo "Connection failed: " . $errorNumber; } else { stream_set_timeout($socket,1); fclose($socket); }
Result: Run the example in an environment that supports the function. Any values depending on operating-system state, installed extensions, process configuration, or network access must be checked on the target server.
Important Behavior and Edge Cases
Connection success depends on the network; set both connection and read timeouts.
Comparison With Related Functions
| Approach | Difference |
|---|---|
fsockopen() | Opens a TCP or UDP client socket using PHP stream APIs. |
| Related operation | stream_socket_client() offers additional transport flags and context options. |
Common Beginner Mistakes
| Mistake | Correct Approach |
|---|---|
| Ignoring the function-specific limitation | Connection success depends on the network; set both connection and read timeouts. |
| Confusing this function with a related operation | stream_socket_client() offers additional transport flags and context options. |
Key Takeaways
- Opens a TCP or UDP client socket using PHP stream APIs.
- Connection success depends on the network; set both connection and read timeouts.
- stream_socket_client() offers additional transport flags and context options.
Use fsockopen() for the specific operation demonstrated above. Check its prerequisites and handle failure conditions before using it with production data.