Php fsockopen close connection

PHP fsockopen() Function: Everything You Need to Know

As a PHP developer, you may need to establish a network connection and communicate with another server using the TCP/IP protocol. In such scenarios, the PHP fsockopen() function comes in handy. It is a built-in function in PHP that allows you to open a network connection to a server and send/receive data through the connection. In this article, we will take an in-depth look at the fsockopen() function and its usage.

What is the fsockopen() Function?

The fsockopen() function is a PHP built-in function that allows you to open a network connection to a server and send/receive data through the connection. It uses the TCP/IP protocol to establish a network connection with the specified host and port.

How to Use the fsockopen() Function

Using the fsockopen() function is straightforward. Here is the syntax of the function:

The function takes five parameters:

  • $hostname: The host name or IP address of the server that you want to connect to.
  • $port: The port number that you want to connect to.
  • &$errno: A variable that stores the error number, if any.
  • &$errstr: A variable that stores the error message, if any.
  • $timeout: The timeout value in seconds for the connection. This parameter is optional and defaults to the default_socket_timeout ini setting if not specified.

Here is an example of how to use the fsockopen() function to establish a network connection with a server and send/receive data:

 $host = "example.com"; $port = 80; $timeout = 30; $fp = fsockopen($host, $port, $errno, $errstr, $timeout); if (!$fp) < echo "Error: $errstr ($errno)
"
; > else < $out = "GET / HTTP/1.1\r\n"; $out .= "Host: $host\r\n"; $out .= "Connection: Close\r\n\r\n"; fwrite($fp, $out); while (!feof($fp)) < echo fgets($fp, 128); > fclose($fp); >

In this example, we use the fsockopen() function to establish a network connection with the server «example.com» on port 80. We specify a timeout of 30 seconds for the connection. If the connection is established successfully, we send an HTTP GET request to the server and receive the response using the fgets() function.

Conclusion

The fsockopen() function is a powerful tool for establishing network connections and sending/receiving data through the connections in PHP. By understanding the syntax and usage of the function, you can easily establish network connections with other servers and communicate with them using the TCP/IP protocol. We hope this article has been informative and useful in understanding the fsockopen() function in PHP.

Источник

PHP fsockopen() Function

$fp = fsockopen(«www.w3schools.com», 80, $errno, $errstr, 20);
if (!$fp) echo «$errstr ($errno)
«;
> else $out = «GET / HTTP/1.1\r\n»;
$out .= «Host: www.w3schools.com\r\n»;
$out .= «Connection: Close\r\n\r\n»;
fwrite($fp, $out);
while (!feof($fp)) echo fgets($fp, 128);
>
fclose($fp);
>
?>

Definition and Usage

The fsockopen() function opens an Internet or Unix domain socket connection.

Syntax

Parameter Values

Parameter Description
hostname Required. Specifies a hostname (like «www.w3schools.com»). ssl:// or tls:// works over TCP/IP to connect to the remote host
port Optional. Specifies the port number. Use -1 for transports that do not use ports, like unix://
errno Optional. Specifies the system level error number
errstr Optional. Specifies the error message as a string
timeout Optional. Specifies the connection timeout (in seconds)

Technical Details

Return Value: A file pointer that can be used with other file functions (such as fgets(), fwrite(), fclose()). FALSE on failure.
PHP Version: 4.0+

❮ PHP Network Reference

Unlock Full Access 50% off

COLOR PICKER

colorpicker

Join our Bootcamp!

Report Error

If you want to report an error, or if you want to make a suggestion, do not hesitate to send us an e-mail:

Thank You For Helping Us!

Your message has been sent to W3Schools.

Top Tutorials
Top References
Top Examples
Get Certified

W3Schools is optimized for learning and training. Examples might be simplified to improve reading and learning. Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. While using W3Schools, you agree to have read and accepted our terms of use, cookie and privacy policy.

Источник

Читайте также:  Java adding items to list
Оцените статью