Php simulates post to submit data

Source: Internet
Author: User
Tags bbpress
Php simulates post to submit data
Php simulates post submission of data, which is of great use and can be used for website Collection and login.

  1. // Use a program to log on to a forum as an example.
  2. Function bbslogin ($ user_login, $ password, $ host, $ port = "80 "){
  3. // The post data to be submitted
  4. $ Argv = array ('cookie '=> array ('User _ login' => $ user_login, 'password' => $ password, '_ wp_http_referer' => '/bbpress/', 're' => '', 'Remember '=> true ));
  5. Foreach ($ argv ['cookies'] 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 ";
  14. $ Header. = $ params;
  15. $ Fp = fsockopen ($ host, $ port );
  16. Fputs ($ fp, $ header );
  17. While (! Feof ($ fp )){
  18. $ Str = fgets ($ fp );
  19. // The following is your own logic code. here we mainly simulate cookies for synchronous 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. ?>

  1. // Three methods of php post data
  2. // Php can post data in three ways: Curl, socket, and file_get_contents:
  3. /**
  4. * Socket version
  5. * Usage:
  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 = 80, $ 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. * Usage:
  35. * $ Post_string = "app = request & version = beta ";
  36. * Request_by_curl ('http: // facebook.cn/restserver.php', then 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. * Usage:
  52. * $ Post_string = "app = request & version = beta ";
  53. * Request_by_other ('http: // facebook.cn/restserver.php', then 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. ?>

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.