


PHP simulates sending POST request 3. Use Telnet and fsockopen() to simulate sending POST information, telnetfsockopen
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.

What’s still popular is the ease of use, flexibility and a strong ecosystem. 1) Ease of use and simple syntax make it the first choice for beginners. 2) Closely integrated with web development, excellent interaction with HTTP requests and database. 3) The huge ecosystem provides a wealth of tools and libraries. 4) Active community and open source nature adapts them to new needs and technology trends.

PHP and Python are both high-level programming languages that are widely used in web development, data processing and automation tasks. 1.PHP is often used to build dynamic websites and content management systems, while Python is often used to build web frameworks and data science. 2.PHP uses echo to output content, Python uses print. 3. Both support object-oriented programming, but the syntax and keywords are different. 4. PHP supports weak type conversion, while Python is more stringent. 5. PHP performance optimization includes using OPcache and asynchronous programming, while Python uses cProfile and asynchronous programming.

PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

PHP originated in 1994 and was developed by RasmusLerdorf. It was originally used to track website visitors and gradually evolved into a server-side scripting language and was widely used in web development. Python was developed by Guidovan Rossum in the late 1980s and was first released in 1991. It emphasizes code readability and simplicity, and is suitable for scientific computing, data analysis and other fields.

PHP is suitable for web development and rapid prototyping, and Python is suitable for data science and machine learning. 1.PHP is used for dynamic web development, with simple syntax and suitable for rapid development. 2. Python has concise syntax, is suitable for multiple fields, and has a strong library ecosystem.

PHP remains important in the modernization process because it supports a large number of websites and applications and adapts to development needs through frameworks. 1.PHP7 improves performance and introduces new features. 2. Modern frameworks such as Laravel, Symfony and CodeIgniter simplify development and improve code quality. 3. Performance optimization and best practices further improve application efficiency.

PHPhassignificantlyimpactedwebdevelopmentandextendsbeyondit.1)ItpowersmajorplatformslikeWordPressandexcelsindatabaseinteractions.2)PHP'sadaptabilityallowsittoscaleforlargeapplicationsusingframeworkslikeLaravel.3)Beyondweb,PHPisusedincommand-linescrip

PHP type prompts to improve code quality and readability. 1) Scalar type tips: Since PHP7.0, basic data types are allowed to be specified in function parameters, such as int, float, etc. 2) Return type prompt: Ensure the consistency of the function return value type. 3) Union type prompt: Since PHP8.0, multiple types are allowed to be specified in function parameters or return values. 4) Nullable type prompt: Allows to include null values and handle functions that may return null values.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment