PHP Analog Post submission data

Source: Internet
Author: User
Tags bbpress
PHP analog post submission data, a lot of useful, can be used for site collection, landing and so on
  1. Log in as a program login as an example
  2. function Bbslogin ($user _login, $password, $host, $port = "80") {
  3. Post data that needs to be submitted
  4. $ARGV = Array (' cookie ' = = Array (' user_login ' = = $user _login, ' password ' + $password, ' _wp_http_referer ' = '/ bbpress/', ' re ', ' = ', ' remember ' = true);
  5. foreach ($argv [' Cookie '] as $key = + $value) {
  6. $params [] = $key. '=' . $value;
  7. }
  8. $params = Implode (' & ', $params);
  9. $header = "post/bbpress/bb-login.php http/1.1\r\n";
  10. $header. = "Host: $host: $port \ r \ n";
  11. $header. = "content-type:application/x-www-form-urlencoded\r\n";
  12. $header. = "Content-length:". Strlen ($params). "\ r \ n";
  13. $header. = "connection:close\r\n\r\n";
  14. $header. = $params;
  15. $fp = Fsockopen ($host, $port);
  16. Fputs ($fp, $header);
  17. while (!feof ($fp)) {
  18. $str = fgets ($FP);
  19. Here is your own logic code, here is the main analog cookie, can be used to synchronize login
  20. if (! ( Strpos ($str, "Set-cookie:") = = = = False)) {
  21. $tmparray = Explode ("", $str);
  22. $cookiearray = explode ("=", $tmparray [1]);
  23. $cookiepaths = explode ("=", $tmparray [6]);
  24. $cookiename = UrlDecode ($cookiearray [0]);
  25. $cookievalue = UrlDecode (substr ($cookiearray [1], 0, strlen ($cookiearray [1])-1));
  26. $cookietime = time () + 3600 * 24 * 7;
  27. $cookiepath = UrlDecode (substr ($cookiepaths [1], 0, strlen ($cookiepaths [1])-1));
  28. Setcookie ($cookiename, $cookievalue, $cookietime, $cookiepath);
  29. }
  30. }
  31. Fclose ($FP);
  32. }
  33. ?>
Copy Code
  1. Three ways to post data in PHP
  2. PHP has three ways to post data, namely curl, socket, file_get_contents:
  3. /**
  4. * Socket version
  5. * How to use:
  6. * $post _string = "App=socket&version=beta";
  7. * Request_by_socket (' facebook.cn ', '/restserver.php ', $post _string);
  8. */
  9. function Request_by_socket ($remote _server, $remote _path, $post _string, $port = up, $timeout = 30)
  10. {
  11. $socket = Fsockopen ($remote _server, $port, $errno, $errstr, $timeout);
  12. if (! $socket) Die ("$errstr ($errno)");
  13. Fwrite ($socket, "POST $remote _path http/1.0\r\n");
  14. Fwrite ($socket, "User-agent:socket example\r\n");
  15. Fwrite ($socket, "HOST: $remote _server\r\n");
  16. Fwrite ($socket, "content-type:application/x-www-form-urlencoded\r\n");
  17. Fwrite ($socket, "Content-length:".) (Strlen ($post _string) + 8). ' \ r \ n ');
  18. Fwrite ($socket, "accept:*/*\r\n");
  19. Fwrite ($socket, "\ r \ n");
  20. Fwrite ($socket, "mypost= $post _string\r\n");
  21. Fwrite ($socket, "\ r \ n");
  22. $header = "";
  23. while ($str = Trim (fgets ($socket, 4096))) {
  24. $header. = $str;
  25. }
  26. $data = "";
  27. while (!feof ($socket)) {
  28. $data. = Fgets ($socket, 4096);
  29. }
  30. return $data;
  31. }
  32. /**
  33. * Curl Version
  34. * How to use:
  35. * $post _string = "App=request&version=beta";
  36. * Request_by_curl (' http://facebook.cn/restServer.php ', $post _string);
  37. */
  38. function Request_by_curl ($remote _server, $post _string)
  39. {
  40. $ch = Curl_init ();
  41. curl_setopt ($ch, Curlopt_url, $remote _server);
  42. curl_setopt ($ch, Curlopt_postfields, ' mypost= '. $post _string);
  43. curl_setopt ($ch, Curlopt_returntransfer, true);
  44. curl_setopt ($ch, Curlopt_useragent, "Jimmy's CURL Example beta");
  45. $data = curl_exec ($ch);
  46. Curl_close ($ch);
  47. return $data;
  48. }
  49. /**
  50. * Other versions
  51. * How to use:
  52. * $post _string = "App=request&version=beta";
  53. * Request_by_other (' http://facebook.cn/restServer.php ', $post _string);
  54. */
  55. function Request_by_other ($remote _server, $post _string)
  56. {
  57. $context = Array (
  58. ' http ' = = Array (
  59. ' Method ' = ' POST ',
  60. ' Header ' = ' content-type:application/x-www-form-urlencoded '.
  61. ' \ r \ n '. ' User-agent:jimmy\ ' s POST Example beta '.
  62. ' \ r \ n '. ' Content-length: '. strlen ($post _string) + 8,
  63. ' Content ' = ' mypost= '. $post _string)
  64. );
  65. $stream _context = stream_context_create ($context);
  66. $data = file_get_contents ($remote _server, False, $stream _context);
  67. return $data;
  68. }
  69. ?>
Copy Code
  • 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.