PHP模拟发送POST请求之三、用Telnet和fsockopen()模拟发送POST信息,telnetfsockopen
PHP模拟发送POST请求之三、用Telnet和fsockopen()模拟发送POST信息,telnetfsockopen
了解完了HTTP头信息和URL信息的具体内容,我们开始尝试自己动手写一段头信息发送到服务器。Windows内置命令Telnet可以帮助我们发送简单的HTTP请求。
并且TELNET是一个特别灵活的工具,我们还可以用它进行FTP/SMTP/POP3/TCP等方式的简单请求。
通过开始菜单--运行--CMD命令进入DOS状态,
我们输入“Telnet 主机地址 端口(Telnet 192.168.1.99 80)” 来进入telnet命令状态(完全黑窗口,此时输出字符会出问题),我们按”ctrl”+”]”,切回普通CMD窗口,再按”ENTER”回车键进入编辑命令状态。
如果系统提示找不到TELNET命令,只需按如下方式可解锁TELNET命令。
进入TELNET 命令输入模式后我们就可以直接编辑头文件来进行发送了
只需要注意:当编辑完请求头文件后要按两次回车键来发送。
这是一次标准的HTTP请求与响应,是不是跟上节的HTTP文件关联了。
当然我们不能只用这么不方便的工具,下面要介绍的是PHP里的fsockopen()方法。
先来看fsockopen()方法的原型:
<p>resource fsockopen ( string $hostname [, int $port = -1 [, int &$errno [, string &$errstr [, float $timeout = ini_get("default_socket_timeout") ]]]] )</p>
它返回一个资源类型的结果,需要传入的参数分别是:
$hostname:主机名
$port:端口号
$errno:错误号(注意原型中的"&"一旦连接有错误,错误号将会被赋值给$errno)
$errstr:错误字符串(一旦连接有错误,错误信息将会被赋值给$errstr)
$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
这个资源类型就如同Telnet里我们用telnet 主机名 端口号 命令之后的环境。
那么接下来,我们就要往这个环境里写内容了。
我们可以分次用fwrite()方法写入头信息,也可以将头信息进行拼合一次写入。
<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");
或先拼合再写入的方式:
<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>);
注意:请求行、报头之间有一次回车键,我们用"\r\n"来进行回车输入,结束输入时两用"\r\n\r\n"模拟两次回车。
接下来我们用fread来读取响应信息并将其存入$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>
当然以上是一个特别简单的例子,我们可以通过拼合更多的报头信息(加入Accept等)来将它更加完善。
接下来我们会说一些常用的POST方式。
如果您觉得本文对您有帮助,请您动手点一下推荐,如果有什么问题,可以在下方留言共同讨论,谢谢。

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

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics











This tutorial demonstrates how to efficiently process XML documents using PHP. XML (eXtensible Markup Language) is a versatile text-based markup language designed for both human readability and machine parsing. It's commonly used for data storage an

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

Static binding (static::) implements late static binding (LSB) in PHP, allowing calling classes to be referenced in static contexts rather than defining classes. 1) The parsing process is performed at runtime, 2) Look up the call class in the inheritance relationship, 3) It may bring performance overhead.

A string is a sequence of characters, including letters, numbers, and symbols. This tutorial will learn how to calculate the number of vowels in a given string in PHP using different methods. The vowels in English are a, e, i, o, u, and they can be uppercase or lowercase. What is a vowel? Vowels are alphabetic characters that represent a specific pronunciation. There are five vowels in English, including uppercase and lowercase: a, e, i, o, u Example 1 Input: String = "Tutorialspoint" Output: 6 explain The vowels in the string "Tutorialspoint" are u, o, i, a, o, i. There are 6 yuan in total

What are the magic methods of PHP? PHP's magic methods include: 1.\_\_construct, used to initialize objects; 2.\_\_destruct, used to clean up resources; 3.\_\_call, handle non-existent method calls; 4.\_\_get, implement dynamic attribute access; 5.\_\_set, implement dynamic attribute settings. These methods are automatically called in certain situations, improving code flexibility and efficiency.

PHP and Python each have their own advantages, and choose according to project requirements. 1.PHP is suitable for web development, especially for rapid development and maintenance of websites. 2. Python is suitable for data science, machine learning and artificial intelligence, with concise syntax and suitable for beginners.

PHP is widely used in e-commerce, content management systems and API development. 1) E-commerce: used for shopping cart function and payment processing. 2) Content management system: used for dynamic content generation and user management. 3) API development: used for RESTful API development and API security. Through performance optimization and best practices, the efficiency and maintainability of PHP applications are improved.

PHP is a scripting language widely used on the server side, especially suitable for web development. 1.PHP can embed HTML, process HTTP requests and responses, and supports a variety of databases. 2.PHP is used to generate dynamic web content, process form data, access databases, etc., with strong community support and open source resources. 3. PHP is an interpreted language, and the execution process includes lexical analysis, grammatical analysis, compilation and execution. 4.PHP can be combined with MySQL for advanced applications such as user registration systems. 5. When debugging PHP, you can use functions such as error_reporting() and var_dump(). 6. Optimize PHP code to use caching mechanisms, optimize database queries and use built-in functions. 7
