Using PHP to simulate post submission data

Source: Internet
Author: User
Tags explode fread vars

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?
  1. <?php
  2. function Pr () {
  3. $params = Func_get_args ();
  4. foreach ($params as $key = = $value) {
  5. echo "<pre>";
  6. Print_r ($value);
  7. echo "</pre>";
  8. }
  9. }
<?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?
    1. <?php
    2. Require dirname (__file__).'  /function.php ';
    3. if (Isset ($_post) &&! Empty ($_post)) {
    4. PR ($_post);
    5. } Else {
    6. PR ("NO POST data!");
    7. }
<?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?
  1. <?php
  2. Require dirname (__file__).'  /function.php ';
  3. function File_get_contents_post ($url, $post) {
  4. $options = Array (
  5. ' http ' = = Array (
  6. ' method ' = ' POST ',
  7. //' content ' = ' name=caiknife&[email protected] ',
  8. ' content ' = Http_build_query ($post),
  9. ),
  10. );
  11. $result = file_get_contents ($url, False, Stream_context_create ($options));
  12. return $result;
  13. }
  14. $data = File_get_contents_post ("http://www.a.com/post/post.php", Array (' name ' = 'caiknife ',  ' email ' = '[email protected] ');
  15. 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?
  1. <?php
  2. Require dirname (__file__).'  /function.php ';
  3. function Curl_post ($url, $post) {
  4. $options = Array (
  5. Curlopt_returntransfer = True,
  6. Curlopt_header = False,
  7. Curlopt_post = True,
  8. Curlopt_postfields = $post,
  9. );
  10. $ch = curl_init ($url);
  11. Curl_setopt_array ($ch, $options);
  12. $result = curl_exec ($ch);
  13. Curl_close ($ch);
  14. return $result;
  15. }
  16. $data = Curl_post ("http://www.a.com/post/post.php", Array (' name ' = 'caiknife ', ' email ' =  ' [email protected] ');
  17. 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?
  1. <?php
  2. Require dirname (__file__).'  /function.php ';
  3. function Socket_post ($url, $post) {
  4. $urls = parse_url ($url);
  5. if (!isset ($urls [' Port ')]) {
  6. $urls [' port '] = 80;
  7. }
  8. $fp = fsockopen ($urls [' host '], $urls [' Port '], $errno, $errstr);
  9. if (! $FP) {
  10. echo "$errno, $errstr";
  11. exit ();
  12. }
  13. $post = http_build_query ($post);
  14. $length = strlen ($post);
  15. $header = <<
  16. POST {$urls [' path ']} http/1.1
  17. Host: {$urls [' Host ']}
  18. content-type:application/x-www-form-urlencoded
  19. Content-length: {$length}
  20. Connection:close
  21. {$post}
  22. HEADER;
  23. Fwrite ($fp, $header);
  24. $result = ";
  25. While (! Feof ($fp)) {
  26. //Receive the results of the request
  27. $result. = fread ($fp, 512);
  28. }
  29. $result = explode ("\r\n\r\n", $result, 2);
  30. return $result [1];
  31. }
  32. $data = Socket_post ("http://www.a.com/post/post.php", Array (' name ' = 'caiknife ', ' email ' =  >' [email protected] ');
  33. 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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.