After understanding the specific content of HTTP header information and URL information, we started to try Write a piece of header information yourself and send it to the server. Windows built-in command Telnet can help us send simple HTTP requests.
And TELNET is a particularly flexible tool. We can also use it to make simple requests such as FTP/SMTP/POP3/TCP.
Enter the DOS state through the Start menu--Run--CMD command,
We enter "Telnet host address port (Telnet 192.168.1.99 80)" to enter the telnet command state (the window is completely black, and there will be problems with outputting characters at this time). We press "ctrl" "]" to switch back to normal CMD window, and then press the "ENTER" key to enter the editing command state.
If the system prompts that the TELNET command cannot be found, just follow the following method to unlock the TELNET command.
After entering the TELNET command input mode, we can directly edit the header file to send it
Just note: after editing the request header file, press the Enter key twice to send it.
This is a standard HTTP request and response. Is it related to the HTTP file in the previous section?
Of course we can’t just use such an inconvenient tool. What we will introduce below is the fsockopen() method in PHP.
Let’s first look at the prototype of the fsockopen() method:
<p>resource fsockopen ( string $hostname [, int $port = -1 [, int &$errno [, string &$errstr [, float $timeout = ini_get("default_socket_timeout") ]]]] )</p>
It returns a resource type result, and the parameters that need to be passed in are:
$hostname: host name
$port: port number
$errno: error number (note the "&" in the prototype. Once there is an error in the connection, the error number will be assigned to $errno)
$errstr: error string (once there is an error in connection, the error message will be assigned to $errstr)
$timeout: timeout
<span>$host</span>=<span>parse_url</span>(<span>$url</span>,PHP_URL_HOST);<span>//</span><span>获取host数据</span> <span>$port</span>=<span>parse_url</span>(<span>$url</span>,PHP_URL_PORT)==<span>null</span> ? 80 : <span>parse_url</span>(<span>$url</span>,PHP_URL_PORT);<span>//</span><span>获取端口数据,如果没有设置则默认为80</span> <span>$path</span>=<span>parse_url</span>(<span>$url</span>,PHP_URL_PATH);<span>//</span><span>获取到path信息后面使用</span> <span>$socket</span>=<span>fsockopen</span>(<span>$host</span>,<span>$port</span>,<span>$errno</span>,<span>$errstr</span>,20);//获取资源类型$socket
This resource type is just like the environment after we use the telnet host name port number command in Telnet.
Then next, we will write content into this environment.
We can use the fwrite() method to write the header information in batches, or we can combine the header information and write it once.
<span>fwrite</span>(<span>$socket</span>, "POST ".<span>$path</span>." HTTP/1.1\r\n"<span>); </span><span>fwrite</span>(<span>$socket</span>, "HOST: localhost\r\n\r\n");
Or combine it first and then write:
<span>$str</span>="POST ".<span>$path</span>." HTTP/1.1\r\n"."HOST: localhost\r\n\r\n"<span>; </span><span>fwrite</span>(<span>$socket</span>,<span>$str</span>);
Note: There is a carriage return key between the request line and the header. We use "rn" to enter the carriage return. When ending the input, use "rnrn" to simulate two Enter again.
Next we use fread to read the response information and store it in $info:
<span>while</span> (!<span>feof</span>(<span>$socket</span>)) { <span>//</span><span>当还有待读取内容</span> <span>$info</span>.= <span>fgets</span>(<span>$socket</span>, 4096); <span>//</span><span>每次读取4096个字节内容,并用.连接符连接到$info中。</span> <span>} </span><span>echo</span> <span>$info</span>;<span>//</span><span>输出返回结果。</span>
Of course the above is a very simple example, we can make it more perfect by combining more header information (adding Accept, etc.).
Next we will talk about some common POST methods.
If you think this article is helpful to you, please click on it to recommend it. If you have any questions, you can leave a message below to discuss together. Thank you.