Home > Backend Development > PHP Tutorial > Introduction to PHP functions—fsockopen(): Open a network connection

Introduction to PHP functions—fsockopen(): Open a network connection

PHPz
Release: 2023-07-24 21:50:01
Original
2014 people have browsed it

PHP function introduction—fsockopen(): Open a network connection

The fsockopen() function is a method in PHP used to open a network connection. It allows us to communicate with remote servers via TCP/IP protocol, send requests and get responses. In this article, we will introduce the fsockopen() function in detail and provide code examples to help readers understand and use the function.

  1. Function syntax:
resource fsockopen ( string $hostname [, int $port = -1 [, int &$errno [, string &$errstr [, float $timeout = ini_get("default_socket_timeout") ]]]] )
Copy after login
  1. Function parameters:
  2. $hostname: The target host address to be connected, which can be an IP address or domain name .
  3. $port: Optional parameter, specify the port number of the target host to connect to. The default value is -1, which means use the default port number.

The following are optional parameters (not commonly used):

  • &$errno: When the connection fails, set the referenced variable to an error code.
  • &$errstr: When the connection fails, set the referenced variable to the error message.
  • $timeout: Optional parameter, specifies the connection timeout, in seconds. The default value is ini_get("default_socket_timeout"), which defaults to the socket timeout set in the PHP configuration.
  1. Function return value:
  2. When successful, a resource handle containing connection information is returned, which can be used by other functions.
  3. On failure, return false.
  4. Example of function usage:

The following is an example of using the fsockopen() function to obtain the content of a remote website:

<?php
// 定义目标服务器地址和端口号
$hostname = 'www.example.com';
$port = 80;

// 打开一个网络连接
$fp = fsockopen($hostname, $port, $errno, $errstr, 10);

// 连接失败时输出错误信息
if (!$fp) {
    echo "Connection failed: $errno - $errstr";
    exit;
}

// 发送 HTTP 请求头
$request = "GET / HTTP/1.1
";
$request .= "Host: $hostname
";
$request .= "Connection: close

";
fwrite($fp, $request);

// 读取响应并输出
while (!feof($fp)) {
    echo fgets($fp, 128);
}

// 关闭连接
fclose($fp);
?>
Copy after login

The above code is created through the fsockopen() function A connection is made to the target server, then a simple HTTP GET request is sent and the response is read. In this example, the host we specified is www.example.com and the port is 80, which is the default port for the HTTP protocol. The Host and Connection fields are set in the request header, and then the request is sent to the server through the fwrite() function. Finally, the response is continuously read through the fgets() function and output through echo.

It is worth noting that if you want to open an SSL encrypted connection, you can use "ssl://" as the prefix of the host name and specify the appropriate port number (usually 443).

Summary:
The fsockopen() function is a powerful tool in PHP for opening network connections. It allows us to communicate with remote servers, send requests and get responses. In this article, we introduce the syntax and parameters of the fsockopen() function and provide a sample code showing how to use it. By mastering and flexibly using the fsockopen() function, we can better handle and manage network communications.

The above is the detailed content of Introduction to PHP functions—fsockopen(): Open a network connection. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template