Previously, PHP curl extensions were used to simulate post requests, and PHP socket can also be used. I recently reviewed the relevant materials and found that it was not so advanced, but I have never fully understood the principles and nature of post, the protocol string sent to the target program as post is as follows:
POST/destination program url HTTP/1.1
Accept: Format of received information
Referer: url
Accept-Language: receiving Language
Content-Type: application/x-www-form-urlencoded
Cookie: no need to explain the website cookie too much, right?
User-Agent: User Agent, operating system and version, CPU type, browser and version
Host: Host address to be sent
Content-Length: the Length of the sent data
Pragma: whether the cache exists locally
Cache-Control: whether the webpage Cache is required
Connection: Connection status
Username = fengdingbo & password = 111cn.net // data sent by post
I think you should not be familiar with submitting data using the form post method. 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 to send the form to the action program. I think it's not hard 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!
Official prototype:
Resource fsockopen (string $ hostname [, int $ port =-1 [, int & $ errno [, string & $ errstr [, float $ timeout = ini_get ("default_socket_timeout")])
Below is human understanding:
Fsockopen (host name, port number, error code & variable, error message & variable, timeout time)
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;
Variable of the error code. This is the error number returned when the socket fails to be created;
The error message returned when an 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, use this open channel to send the post request protocol to the target program. At this time, you can use any of the fwrite or fputs functions to send the post request format to the resource handle opened by fsockopen, at this time, a great socket simulated post request was born.
The code is as follows: |
Copy code |
<? Php /** * SOCKET extended functions * @ Copyright (c) 2013 * @ Author Qiufeng <fengdingbo@gmail.com> * @ Link http://www.111cn.net * @ Version 1.0 */ /** * Post Request * * @ Param string $ url * @ Param array $ data * @ Param string $ referer * @ Return array */ If (! Function_exists ('socket _ Post ')) { Function socket_post ($ url, $ data, $ referer = '') { If (! Is_array ($ data )) { Return; } $ Data = http_build_query ($ data ); $ Url = parse_url ($ url ); If (! Isset ($ url ['scheme ']) | $ url ['scheme']! = 'Http ') { Die ('Error: Only HTTP request are supported! '); } $ Host = $ url ['host']; $ Path = isset ($ url ['path'])? $ Url ['path']: '/'; // Open a socket connection on port 80-timeout: 30 sec $ Fp = fsockopen ($ host, 80, $ errno, $ errstr, 30 ); If ($ fp) { // Send the request headers: $ Length = strlen ($ data ); $ POST = <HEADER POST {$ path} HTTP/1.1 Accept: text/plain, text/html Referer: {$ referer} Accept-Language: zh-CN, zh; q = 0.8 Content-Type: application/x-www-form-urlencodem Cookie: token = value; pub_cookietime = 2592000; pub_sauth1 = value; pub_sau2= value User-Agent: Mozilla/5.0 (X11; Linux i686) AppleWebKit/537.17 (KHTML, like Gecko) Chrome/24.0.1312.56 Safari/537.17 Host: {$ host} Content-Length: {$ length} Pragma: no-cache Cache-Control: no-cache Connection: closern {$ Data} HEADER; Fwrite ($ fp, $ POST ); $ Result = ''; While (! Feof ($ fp )) { // Receive the results of the request $ Result. = fread ($ fp, 512 ); } } Else { Return array ( 'Status' => 'error ', 'Error' => "$ errstr ($ errno )" ); } // Close the socket connection: Fclose ($ fp ); // Split the result header from the content $ Result = explode ("rnrn", $ result, 2 ); // Return as structured array: Return array ( 'Status' => 'OK ', 'Header' => isset ($ result [0])? $ Result [0]: '', 'Content' => isset ($ result [1])? $ Result [1]:'' ); } } Print_r (socket_post ('http: // www.111cn.net/', array ('name =' => 'qiufeng ', 'password' => md5 ('www .111cn.net ')))); /* E. g: socket_post ('http: // www.111cn.net ', array ('name =' => 'qiufeng', 'password' => md5 ('111cn. ')));*/ /* End of file socket_helper.php */ |
In fact, when the socket channel is opened, the COOKIE we pass is correct (the php code run in the screenshot comes from the top, and my user name appears on the webpage returned after the operation, it indicates that the website of the other party has admitted that I have logged on. You can do more than N Things, for example, post and reply. You understand, right?
Well, the above is not convincing enough. Let's look at a php socket to upload images.
This code has two points to note:
First, it is an http post request;
Second, form Upload protocol,
The request string is applicable to any language.
The code is as follows: |
Copy code |
<? Php $ Remote_server = "111cn.net "; $ Boundary = "---------------------". substr (md5 (rand ); // Build the header $ Header = "POST/api. php? Action = twupload HTTP/1.0rn "; $ Header. = "Host: {$ remote_server} rn "; $ Header. = "Content-type: multipart/form-data, boundary = $ boundaryrn "; /* // Attach post vars Foreach ($ _ post as $ index => $ value ){ $ Data. = "-- $ boundaryrn "; $ Data. = "Content-Disposition: form-data; name =" ". $ index." "rn "; $ Data. = "rn". $ value. "rn "; $ Data. = "-- $ boundaryrn "; } */ $ File_name = "aaa.jpg "; $ Content_type = "image/jpg "; $ Data = ''; // And attach the file $ Data. = "-- $ boundaryrn "; $ Content_file = file_get_contents('aaa.jpg '); $ Data. = "Content-Disposition: form-data; name =" userfile "; filename =" $ file_name "rn "; $ Data. = "Content-Type: $ content_typernrn "; $ Data. = "". $ content_file. "rn "; $ Data. = "-- $ boundary -- rn "; $ Header. = "Content-length:". strlen ($ data). "rnrn "; // Open the connection $ Fp = fsockopen ($ remote_server, 80 ); // Then just Fputs ($ fp, $ header. $ data ); // Reader While (! Feof ($ fp )){ Echo fgets ($ fp, 128 ); } Fclose ($ fp ); |