Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How does PHP socket simulate a POST request

2025-04-11 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article introduces the knowledge of "how PHP socket simulates POST requests". Many people will encounter this dilemma in the operation of actual cases, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

I used to simulate post requests using PHP curl extensions, but I never thought PHP socket could also be implemented. Recently, I looked up the relevant information and found that it was not so advanced, but I did not fully understand the principle and nature of post before. in fact, it was sent to the destination program with a protocol string marked post as follows:

POST / destination program url HTTP/1.1

Accept: receive message format

Referer: where url comes from

Accept-Language: receiving language

Content-Type: application/x-www-form-urlencoded

Cookie: website cookie, I don't need to explain too much, right?

User-Agent: user agent, operating system and version, CPU type, browser and version, etc.

Host: the host address to send to

Content-Length: the length of the data sent

Pragma: whether a cache exists locally

Cache-Control: whether web page caching is required

Connection: connection statu

Data sent by username=fengdingbo&password=jb51.net / / post

I think you are most familiar with the post method of the form to submit data. For example, when we want to send the user name and password to a page, fill in the corresponding input box, click the submit button, and finally send the form to the action program with the above data. I don't think it will be difficult to know that.

At this time, we only need to use php's socket to open a port, such as port 80, and send the above information to the destination program using this port.

How do we set up a socket channel on a port?

It's so simple in PHP!

Official prototype:

Resource fsockopen (string $hostname [, int $port =-1 [, int & $errno [, string & $errstr [, float $timeout = ini_get ("default_socket_timeout")])

Here is the human understanding:

Fsockopen (host name, port number, & variable of error number, & variable of error prompt, timeout)

The host name is the destination where you need to send data.

The port number is the port on which the destination program is waiting for your data.

The & variable of the error number, which is the error number returned if the establishment of the socket is not successful

The & variable of the error prompt is the error message returned when the error occurs.

The timeout is the maximum time to wait after post data if the other party does not respond to the message.

If nothing happens (you correctly set the parameters of the fsockopen () function), a socket channel is now open, and the next step we need to do is to send the post request protocol to the destination program through this open channel. At this time, you can use any one of the fwrite or fputs function to send the post request format to the fsockopen () open resource handle, and a great socket simulated post request will be born.

The code is as follows

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report