ホームページ  >  記事  >  バックエンド開発  >  PHP は送信後のデータをシミュレートします

PHP は送信後のデータをシミュレートします

WBOY
WBOYオリジナル
2016-07-25 09:09:061020ブラウズ
PHP は投稿後のデータをシミュレートし、Web サイトの収集やログインなどに使用できます。
  1. //フォーラムにログインするプログラムを例に挙げます
  2. function bbslogin($user_login, $password, $host, $port = "80") {
  3. //データを投稿送信する必要があります
  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 :$portrn";
  11. $header .= "Content-Type: application/x-www-form-urlencodedrn";
  12. $header .= "Content-Length: " . strlen($params) . "rn";
  13. $ header .= "接続: Closernrn";
  14. $header .= $params;
  15. $fp = fsockopen($host, $port);
  16. fputs($fp, $header);
  17. while (!feof($fp)) {
  18. $str = fgets ($fp);
  19. //以下は私独自のロジック コードです。これは主に Cookie をシミュレートし、同期ログインに使用できます
  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. ?>
コードをコピー
    //PHP POST データの 3 つのメソッド
  1. //PHP には、データを投稿するための 3 つのメソッド、つまり Curl、socket、file_get_contents:
  2. /**
  3. * ソケットのバージョン
  4. * 使用法:
  5. * $post_string = "app=socket&version=beta";
  6. * request_by_socket('facebook.cn','/restServer.php',$post_string);
  7. */
  8. function request_by_socket( $remote_server, $remote_path, $post_string, $port = 80, $timeout = 30)
  9. {
  10. $socket = fsockopen($remote_server, $port, $errno, $errstr, $timeout);
  11. if (!$ ソケット) die("$errstr($errno)");
  12. fwrite($socket, "POST $remote_path HTTP/1.0rn");
  13. fwrite($socket, "ユーザーエージェント: ソケットの例rn");
  14. fwrite ($ソケット, "HOST: $remote_serverrn");
  15. fwrite($socket, "Content-type: application/x-www-form-urlencodedrn");
  16. fwrite($socket, "Content-length: " . (strlen ($ post_string) + 8) . 'rn');
  17. fwrite($socket, "Accept:*/*rn");
  18. fwrite($socket, "rn");
  19. fwrite($socket, "mypost=$ post_stringrn" );
  20. fwrite($socket, "rn");
  21. $header = "";
  22. while ($str = trim(fgets($socket, 4096))) {
  23. $header .= $str;
  24. }
  25. $ data = "";
  26. while (!feof($socket)) {
  27. $data .= fgets($socket, 4096);
  28. }
  29. return $data;
  30. }
  31. /**
  32. * Curl バージョン本
  33. * 使用方法:
  34. * $post_string = "app=request&version=beta";
  35. * request_by_curl('http://facebook.cn/restServer.php',$post_string);
  36. */
  37. function request_by_curl($remote_server, $post_string)
  38. {
  39. $ch =curl_init();
  40. curl_setopt($ch, CURLOPT_URL, $remote_server);
  41. curl_setopt($ch, CURLOPT_POSTFIELDS, 'mypost=' . $post_string) ;
  42. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  43. curl_setopt($ch, CURLOPT_USERAGENT, "Jimmy's CURL Example beta");
  44. $data =curl_exec($ch);
  45. curl_close($ch);
  46. return $data;
  47. /**
  48. * 他のバージョン
  49. * 使用方法:
  50. * $post_string = "app=request&version=beta";
  51. * request_by_other('http://facebook.cn/restServer.php',$post_string);
  52. */
  53. function request_by_other($remote_server, $post_string)
  54. {
  55. $context = array(
  56. 'http' => array(
  57. 'method' => 'POST' ,
  58. 'header' => 'Content-type: application/x-www-form-urlencoded' .
  59. 'rn'.'User-Agent : Jimmy's POST Example beta' .
  60. 'rn'.'Content-length: ' . ($post_string) + 8,
  61. 'content' => 'mypost=' . $post_string)
  62. );
  63. $stream_context = stream_context_create($context);
  64. $data = file_get_contents($remote_server, false, $stream_context);
  65. return $data;
  66. }
  67. ?>
コードをコピー


声明:
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。