首頁 > 後端開發 > php教程 > php模擬post提交數據

php模擬post提交數據

WBOY
發布: 2016-07-25 09:09:06
原創
1097 人瀏覽過
php模擬post提交資料,用處很多,可用來網站的採集,登陸等等
  1. //以程式登陸一個論壇登入為例
  2. function bbslogin($user_login, $password, $host, $password, $host, $port = " 80") {
  3. //需要提交的post資料
  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. //以下是自己的邏輯程式碼,這裡主要是模擬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. ?>
複製程式碼
複製程式碼
  1. // PHP POST資料的三種方法
  2. // php有三種方法可以post資料,分別為Curl、socket、file_get_contents:
  3. /**
  4. * Socket版本
  5. * 使用方法:
  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. {
  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_in ;
  41. curl_setopt($ch, CURLOPT_URL, $remote_server);
  42. curl_setopt($ch, CURLOPT_POSTFIELDS, 'mypost=' . $post_string);
  43. curl_setopt($ch, CUR3); curl_setopt($ch, CURLOPT_USERAGENT, "Jimmy's CURL Example beta");
  44. $data = curl_exec($ch);
  45. curl_close($ch);
  46. return $data
  47. curl_close($ch);
  48. return $data
  49. ;
  50. }
  51. /**
  52. * 其它版本
  53. * 使用方法:
  54. * $post_string = "app=request&version=beta";
  55. * request_by_other('http://facebook.cn/restServer.php',$post_string );
  56. */
  57. function request_by_other($remote_server, $post_string)
  58. {
  59. $context = array(
  60. 'http' => array(
  61. 'method' => 'POST',
  62. 'header' => 'Content-type: application/x-www-form-urlencoded' .
  63. 'rn'.'User-Agent : Jimmy's POST Example beta ' .
  64. 'rn'.'Content-length:' . strlen($post_string) + 8,
  65. 'content' => 'mypost=' . $post_string)
  66. );
$stream_context = stream_context_create($context);
$data = file_get_contents($remote_server, false, $stream_context); return $data;}
?>? 🎜>複製程式碼


相關標籤:
來源:php.cn
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板