using PHP to simulate post submission dataCategory: PHP LAMP 2013-04-13 12:03 3954 people read reviews (0) favorite reports curlsocketphp
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 Plaincopyprint?
- <?php
- function Pr () {
- $params = Func_get_args ();
- foreach ($params as $key = = $value) {
- echo "<pre>";
- Print_r ($value);
- echo "</pre>";
- }
- }
<?phpfunction 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 Plaincopyprint?
- <?php
- Require dirname (__file__).' /function.php ';
- if (Isset ($_post) &&! Empty ($_post)) {
- PR ($_post);
- } Else {
- PR ("NO POST data!");
- }
<?phprequire 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 Plaincopyprint?
- <?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);
<?phprequire 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 Plaincopyprint?
- <?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);
<?phprequire 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 Plaincopyprint?
- <?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);
<?phprequire dirname (__file__). ' /function.php '; function Socket_post ($url, $post) { $urls = Parse_url ($url); if (!isset ($urls [' Port ')]) { $urls [' port '] = n; } $fp = Fsockopen ($urls [' Host '], $urls [' Port '], $errno, $errstr); if (! $fp) { echo "$errno, $errstr"; Exit (); } $post = Http_build_query ($post); $length = strlen ($post); $header = <<
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.Using PHP to simulate post submission data