Master PHP’s fsockopen(): Syntax, Parameters, and Real‑World Example

This article explains how PHP's fsockopen() function works, detailing its syntax, parameters, return values, and providing a complete example that demonstrates opening a TCP connection, sending an HTTP request, and handling the response, including notes on SSL usage.

php Courses
php Courses
php Courses
Master PHP’s fsockopen(): Syntax, Parameters, and Real‑World Example
fsockopen()

is a PHP function used to open a network connection via TCP/IP, allowing communication with remote servers. This article introduces the function and provides code examples.

Function Syntax:

resource fsockopen ( string $hostname [, int $port = -1 [, int &$errno [, string &$errstr [, float $timeout = ini_get("default_socket_timeout") ]]]] )

Function Parameters:

$hostname

: target host address (IP or domain). $port: optional port number, default -1 (uses default port). &$errno: variable set to error code on failure. &$errstr: variable set to error message on failure. $timeout: optional timeout in seconds, default ini_get("default_socket_timeout").

Function Return Value:

On success, returns a resource handle containing connection information, which can be used by other functions.

On failure, returns false.

Function Usage Example:

The following example uses fsockopen() to retrieve content from a remote website:

<?php
// Define target server and port
$hostname = 'www.example.com';
$port = 80;

// Open a network connection
$fp = fsockopen($hostname, $port, $errno, $errstr, 10);

if (! $fp) {
    echo "Connection failed: $errno - $errstr";
    exit;
}

// Send HTTP request header
$request = "GET / HTTP/1.1
";
$request .= "Host: $hostname
";
$request .= "Connection: close
";

fwrite($fp, $request);

// Read and output response
while (!feof($fp)) {
    echo fgets($fp, 128);
}

// Close connection
fclose($fp);
?>

This code creates a connection to www.example.com on port 80, sends a simple HTTP GET request, and reads the response.

Note: To open an SSL‑encrypted connection, prepend ssl:// to the hostname and use the appropriate port (usually 443). fsockopen() is a powerful tool for network communication in PHP, and mastering its syntax and parameters enables more flexible handling of remote requests.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

PHPNetwork programmingSocketfsockopen
php Courses
Written by

php Courses

php中文网's platform for the latest courses and technical articles, helping PHP learners advance quickly.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.