It was a cliché, and it took some time in the morning to tidy up the problem.
In general, there are three ways to simulate post submissions with PHP, file_get_contents, curl, and socket.
A common function was written to print the post data:
[PHP]View Plaincopy
- <?php
- function Pr () {
- $params = Func_get_args ();
- foreach ($params as $key = = $value) {
- echo "<pre>";
- Print_r ($value);
- echo "</pre>";
- }
- }
First write a post.php to receive the post data and print it out:
[PHP]View Plaincopy
- <?php
- Require dirname (__file__).' /function.php ';
- if (Isset ($_post) &&! Empty ($_post)) {
- PR ($_post);
- } Else {
- PR ("NO POST data!");
- }
The following is a file_get_contents to simulate post:
[PHP]View Plaincopy
- <?php
- Require dirname (__file__).' /function.php ';
- function File_get_contents_post ($url, $post) {
- $options = Array (
- ' http ' = = Array (
- ' method ' = ' POST ',
- //' content ' = ' name=caiknife&[email protected] ',
- ' content ' = Http_build_query ($post),
- ),
- );
- $result = file_get_contents ($url, False, Stream_context_create ($options));
- return $result;
- }
- $data = File_get_contents_post ("http://www.a.com/post/post.php", Array (' name ' = 'caiknife ', ' Email ' = '[email protected] ');
- Var_dump ($data);
It's simple, isn't it? Look again at the Curl Analog post:
[PHP]View Plaincopy
- <?php
- Require dirname (__file__).' /function.php ';
- function Curl_post ($url, $post) {
- $options = Array (
- Curlopt_returntransfer = True,
- Curlopt_header = False,
- Curlopt_post = True,
- Curlopt_postfields = $post,
- );
- $ch = curl_init ($url);
- Curl_setopt_array ($ch, $options);
- $result = curl_exec ($ch);
- Curl_close ($ch);
- return $result;
- }
- $data = Curl_post ("http://www.a.com/post/post.php", Array (' name ' = 'caiknife ', ' email ' = ' [email protected] ');
- Var_dump ($data);
Finally, the socket is used to simulate the post:
[PHP]View Plaincopy
- <?php
- Require dirname (__file__).' /function.php ';
- function Socket_post ($url, $post) {
- $urls = parse_url ($url);
- if (!isset ($urls [' Port ')]) {
- $urls [' port '] = 80;
- }
- $fp = fsockopen ($urls [' host '], $urls [' Port '], $errno, $errstr);
- if (! $FP) {
- echo "$errno, $errstr";
- exit ();
- }
- $post = http_build_query ($post);
- $length = strlen ($post);
- $header = <<
- POST {$urls [' path ']} http/1.1
- Host: {$urls [' Host ']}
- content-type:application/x-www-form-urlencoded
- Content-length: {$length}
- Connection:close
- {$post}
- HEADER;
- Fwrite ($fp, $header);
- $result = ";
- While (! Feof ($fp)) {
- //Receive the results of the request
- $result. = fread ($fp, 512);
- }
- $result = explode ("\r\n\r\n", $result, 2);
- return $result [1];
- }
- $data = Socket_post ("http://www.a.com/post/post.php", Array (' name ' = 'caiknife ', ' email ' = >' [email protected] ');
- Var_dump ($data);
The last thing you see in these three methods is the same, but when you use a socket, you have to pay attention to the complete information of the header, such as the content type and content length, connection: There is a blank line between the close and post data, and so on, and the content obtained through the socket contains the header information, to be processed to get the real content.
Php-post Simulation (excerpt from the network)