php模拟post提交数据

WBOY
发布: 2016-07-25 09:09:06
原创
1078 人浏览过
php模拟post提交数据,用处很多,可用来网站的采集,登陆等等
  1. //以程序登陆一个论坛登录为例
  2. function bbslogin($user_login, $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.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. //以下是自己的逻辑代码,这里主要是模拟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. $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版本
  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. * 其它版本
  51. * 使用方法:
  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. ?>
复制代码


相关标签:
来源:php.cn
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责声明 Sitemap
PHP中文网:公益在线PHP培训,帮助PHP学习者快速成长!