The essence of post is to sendProgramA protocol string marked as post is as follows:
Post/destination program HTTP/1.1 accept: */* Referer: http://www.phpiask.comAccept-language: ZH-CN, en-US; q = 0.5content-type: application/X-WWW-form-urlencodeduser-AGENT: Mozilla/4.0 (compatible; MSIE 5.0; Windows NT ;. net CLR 1.0.3705 ;. net CLR 1.1.4322) Host: Content-Length of the host address to be sent: length of the sent data Pragma: No-cachecache-control: no-cacheusername = PHP & Password = iask // data sent by post
I think you should not be familiar with the form post method. For example, when sending a user name and password to a page, click the submit button for a form with two text fields, the above data is sent to the action program. I think it is not difficult to understand this.
At this time, we only need to use the PHP socket to open a port, such as port 80, to send the above information to the target program.
How can we establish a socket channel on a port?
It's so easy in PHP!
Fsockopen (host name, port number, accept variable of error code, accept variable of error message, timeout)
The host name is the destination of the data to be sent;
The port number is the port on which the target program will wait for your data;
The accept variable of the error number. This is the error number returned when the socket fails to be created;
The variable for the error message. It is the error message returned when the error occurs;
The timeout time is the maximum waiting time if the recipient does not respond after the post data.
If there is no accident (you set the fsockopen () function parameter correctly), a socket channel is now open. What we need to do next is, send the POST request protocol to the Target Program through this open channel. At this time, you can use either fwrite or any one of the fputs functions to send the POST request format to the handle opened by fsockopen, at this time, a great socket simulated POST request was born. By the way:
When you send a POST request, can you directly press Ctrl + V in the format of my POST request? You didn't see the Chinese characters in it. Do you want to enter them by yourself?
The above is the description of the text. If you think it is very spam and unconvincing, there is a ready-made program snippet below. You can check it out.
$ Port = $ URL ['Port']? $ URL ['Port']: 80; $ fp = fsockopen ($ URL ['host'], $ port, $ errno, $ errstr, 10); If (! $ FP) Return "failed to open socket on Host: $ URL [host] $ port because: $ errno-$ errstr"; fputs ($ FP, sprintf ("post % S % s HTTP/1.0 \ r \ n", $ URL ['path'], $ URL ['query']? "? ":" ", $ URL ['query']); fputs ($ FP," Host :". $ URL ['host']. "\ r \ n"); fputs ($ FP, "Content-Type: Application/X-WWW-form-urlencoded \ r \ n"); fputs ($ FP, "Content-Length :". strlen ($ encoded ). "\ r \ n"); fputs ($ FP, "connection: Close \ r \ n"); fputs ($ FP, "$ encoded \ n ");
By the way, fgets ($ FP, 1024); returns the value !! More than one person has asked me how I accept the returned results !!