Php-post Simulation (excerpt from the network)

Source: Internet
Author: User
Tags vars

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
  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. }


First write a post.php to receive the post data and print it out:

[PHP]View Plaincopy
    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. }


The following is a file_get_contents to simulate post:

[PHP]View Plaincopy
  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);


It's simple, isn't it? Look again at the Curl Analog post:

[PHP]View Plaincopy
  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);


Finally, the socket is used to simulate the post:

[PHP]View Plaincopy
  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);


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)

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.