PHP sends POST request 3, uses Telnet and fsockopen () to send POST information, and uses telnetfsockopen. PHP sends POST request 3, uses Telnet and fsockopen () to send POST information. telnetfsockopen understands the specific content of HTTP header information and URL information, we started to try to use PHP to simulate POST request 3, use Telnet and fsockopen () to simulate POST information sending, telnetfsockopen
After learning about the specific content of the HTTP header and URL information, we began to try to write a piece of header information to the server. Windows built-in Telnet command can help us send simple HTTP requests.
TELNET is a very flexible tool. we can also use it for simple requests in FTP, SMTP, POP3, TCP, and other ways.
Use the Start menu -- run -- CMD command to enter the DOS status,
We enter "Telnet host address port (Telnet 192.168.1.99 80)" to enter the telnet command status (completely black window, the output character may be faulty). press "ctrl" + "]", switch back to the common CMD window and press ENTER to ENTER the edit command status.
If the system prompts that The TELNET command cannot be found, you can unlock the TELNET command as follows.
After entering the TELNET command input mode, we can directly edit the header file for sending.
Note: After editing the request header file, press the Enter key twice.
This is a standard HTTP request and response. is it associated with the HTTP file in the following section.
Of course, we can't just use this inconvenient tool. The following describesFsockopen ()Method.
Let's first look at the prototype of the fsockopen () method:
resource fsockopen ( string $hostname [, int $port = -1 [, int &$errno [, string &$errstr [, float $timeout = ini_get("default_socket_timeout") ]]]] )
It returns the result of a resource type. the parameters to be passed in are:
$ Hostname: host name
$ Port: port number
$ Errno: error number (note that once a connection error occurs in the prototype "&", the error number will be assigned to $ errno)
$ Errstr: error string (once an error occurs during connection, the error message will be assigned to $ errstr)
$ Timeout: timeout
$ Host = parse_url ($ url, PHP_URL_HOST); // Obtain host Data $ port = parse_url ($ url, PHP_URL_PORT) = null? 80: parse_url ($ url, PHP_URL_PORT); // Obtain port data. If no port is set, the default value is 80 $ path = parse_url ($ url, PHP_URL_PATH ); // use $ socket = fsockopen ($ host, $ port, $ errno, $ errstr, 20) after obtaining the path information; // Obtain the resource type $ socket
The resource type is similar to the environment in which we use the Telnet host name and port number command in telnet.
Next, we will write content to this environment.
We can use the fwrite () method to write the header information in multiple times, or combine the header information for one write.
fwrite($socket, "POST ".$path." HTTP/1.1\r\n"); fwrite($socket, "HOST: localhost\r\n\r\n");
Or the method of first splicing and then writing:
$str="POST ".$path." HTTP/1.1\r\n"."HOST: localhost\r\n\r\n";fwrite($socket,$str);
Note: There is a carriage return key between the request line and header. We use"\ R \ n"To press Enter. when the input ends, use "\ r \ n" to simulate two carriage returns.
Next, we use fread to read the response information and store it in $ info:
While (! Feof ($ socket) {// when there is still content to be read $ info. = fgets ($ socket, 4096); // read 4096 bytes each time, and use. connect to $ info .} Echo $ info; // output the returned result.
Of course, the above is a very simple example. we can combine more header information (including Accept) to improve it.
Next we will talk about some common POST methods.
If you think this article is helpful to you, please click here. if you have any questions, you can leave a message below to discuss them. thank you.
Parse () simulates sending POST information. after telnetfsockopen understands the specific content of the HTTP header and URL information, we start to try it on our own...