We use the most analog post requests are almost all using PHP curl to achieve, not considering the PHP socket can also be implemented, today see a friend wrote an article, let me share with you the PHP socket simulation POST request instance.
I used the PHP curl extension to implement the previous analog post request, and I didn't think PHP sockets could be implemented. Recently turned over the relevant information to find that the original is not so advanced, but has not fully understood the post principle and essence, is actually sent to the destination program a sign for the post protocol string as follows:
POST/Destination Program URL http/1.1
Accept: Receive Information Format
Referer:url Route
Accept-language: Receiving language
content-type:application/x-www-form-urlencoded
Cookies: Website cookies, no need to explain too much, right?
User-agent: User agent, operating system and version, CPU type, browser and version information
Host: The address to be sent to
Content-length: Length of data sent
Pragma: Whether there is a local cache
Cache-control: Whether to require Web caching
Connection: Connection Status
username=fengdingbo&password=bkjia.c0m data sent by//post
I think we should be most familiar with the form of post method submission data, for example, we want to send a user name and password to a page, fill in the appropriate input box, click the Submit button, and finally send this form to the action program is the above data. I don't think it's hard to know that.
At this time we just need to open a port with PHP socket, such as 80 port, the above information to use this port to send to the destination program on the line.
How do we build a socket channel on a port?
It's so simple in PHP!
An official prototype:
Resource Fsockopen (string $hostname [, int $port =-1 [, int & $errno [, String & $errstr [, float $timeout = ini_ Get ("Default_socket_timeout")]])
Below is the human understanding:
Fsockopen (host name, port number, error number & variable, error & variable, time-out)
The host name is the destination where you need to send the data;
The port number is the destination where the program will wait for your data;
The & variable of the error number, which is the error number that is returned if the socket is not successfully established;
Error prompt & variable, error message when error is returned;
The time-out is the maximum time to wait after the post data if the other party does not respond to the message.
If there is no accident (you set the parameters of the Fsockopen () function correctly), a socket channel is now open, and what we need to do next is to send the POST request protocol to the destination program through this open channel, At this point, you can use either the fwrite or the fputs function to send the POST request format to the Fsockopen () open resource handle, when a great socket simulation of the post request was born.
The code is as follows |
Copy Code |
/** * Socket extension function * @copyright (c) 2013 * @author Qiufeng * @link http://www.bKjia.c0m * @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 is supported! '); } $host = $url [' Host ']; $path = Isset ($url [' path '])? $url [' Path ']: '/'; Open a socket connection on port 80-timeout:30 sec $fp = Fsockopen ($host, $errno, $errstr, 30); if ($FP) { Send the request headers: $length = strlen ($data); $POST = << <> 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_sauth2=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://hzhuti.com/', Array (' name= ' = ' qiufeng ', ' Password ' =>md5 (' www.bKjia.c0m '))); /* E.g:socket_post (' http://www.bKjia.c0m ', Array (' name= ' = ' qiufeng ', ' Password ' =>md5 (' bkjia.c0m ')); */ /* End of File socket_helper.php */ |
In fact, when the socket channel opens, we pass the cookie is correct, (running PHP code from the top, the page returned after the run of my user name, indicating that the other site has admitted that I have logged in) we can do more than n things, such as brush posts, brush replies, etc., you understand, right?
Well, it's not convincing enough. Let's see a PHP socket implementation picture upload
This code has two points to note
One is that he is the post request for HTTP;
The second is the form upload protocol,
The request string below is suitable for any language.
The code is as follows |
Copy Code |
$remote _server = "bkjia.c0m"; $boundary = "---------------------". SUBSTR (MD5 (rand (0,32000)), 0,10); 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); |
http://www.bkjia.com/PHPjc/633062.html www.bkjia.com true http://www.bkjia.com/PHPjc/633062.html techarticle we use the most analog post requests are almost all using PHP curl to achieve, do not consider the PHP socket can also be implemented, today to see a friend wrote an article, let me give you to share a bit ...