Home  >  Article  >  Backend Development  >  PHP simulates post submission data

PHP simulates post submission data

WBOY
WBOYOriginal
2016-07-25 09:09:061079browse
PHP simulates post submission data, which has many uses. It can be used for website collection, login, etc.
  1. //Take the program to log in to a forum 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.1rn";
  10. $header .= "Host:$host:$portrn";
  11. $header .= "Content-Type: application/x-www-form-urlencodedrn";
  12. $header .= "Content-Length: " . strlen($params) . "rn";
  13. $header .= "Connection: Closernrn";
  14. $header .= $params;
  15. $fp = fsockopen($host, $port);
  16. fputs($fp, $header);
  17. while (!feof($fp)) {
  18. $str = fgets ($fp);
  19. //The following is my own logic code, which mainly simulates cookies and can be used to log in synchronously
  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 methods of PHP POST data
  2. //PHP has three methods to post data, namely Curl, socket, 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.0rn");
  14. fwrite($socket, "User-Agent: Socket Examplern");
  15. fwrite ($socket, "HOST: $remote_serverrn");
  16. fwrite($socket, "Content-type: application/x-www-form-urlencodedrn");
  17. fwrite($socket, "Content-length: " . (strlen ($post_string) + 8) . 'rn');
  18. fwrite($socket, "Accept:*/*rn");
  19. fwrite($socket, "rn");
  20. fwrite($socket, "mypost=$ post_stringrn");
  21. fwrite($socket, "rn");
  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版本
  34. * 使用方法:
  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. 'rn'.'User-Agent : Jimmy's POST Example beta' .
  62. 'rn'.'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


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn