Discuz教程网

php模拟post提交数据,用处很多,可用来网站的采集,登陆等等

[复制链接]
authicon dly 发表于 2012-2-15 22:37:03 | 显示全部楼层 |阅读模式
  1. php模拟post提交数据,用处很多,可用来网站的采集,登陆等等


  2. //以我项目中的论坛登录为例
  3. function A_bbslogin($user_login,$password,$host,$port="80"){
  4. //需要提交的post数据
  5. $argv = array(
  6. 'cookie' => array('user_login' =>$user_login, 'password' => $password,'_wp_http_referer'=>'/bbpress/','re'=>'','remember'=>true)
  7. );
  8. foreach($argv['cookie'] as $key => $value) {
  9. $params[] = $key . '=' . $value;
  10. }
  11. $params = implode('&', $params);
  12. $header = "POST /bbpress/bb-login.php HTTP/1.1\r\n";
  13. $header .= "Host:$host:$port\r\n";
  14. $header .= "Content-Type: application/x-www-form-urlencoded\r\n";
  15. $header .= "Content-Length: " . strlen($params) . "\r\n";
  16. $header .= "Connection: Close\r\n\r\n";
  17. $header .= $params;
  18. $fp = fsockopen($host, $port);
  19. fputs($fp, $header);
  20. while(!feof($fp)) {
  21. $str = fgets($fp); //以下是自己的逻辑代码,这里主要是模拟cookie,可用来同步登陆
  22. if(!(strpos($str,"Set-Cookie:") === false)){
  23. $tmparray = explode(" ",$str);
  24. $cookiearray = explode("=",$tmparray[1]);
  25. $cookiepaths = explode("=",$tmparray[6]);
  26. $cookiename = urldecode($cookiearray[0]);
  27. $cookievalue = urldecode(substr($cookiearray[1],0,strlen($cookiearray[1])-1));
  28. $cookietime = time()+3600*24*7;
  29. $cookiepath = urldecode(substr($cookiepaths[1],0,strlen($cookiepaths[1])-1));
  30. setcookie($cookiename,$cookievalue,$cookietime,$cookiepath);
  31. }
  32. }
  33. fclose($fp);
  34. }
复制代码
  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. $socket = fsockopen($remote_server,$port,$errno,$errstr,$timeout);
  11. if (!$socket) die("$errstr($errno)");

  12. fwrite($socket,"POST $remote_path HTTP/1.0\r\n");
  13. fwrite($socket,"User-Agent: Socket Example\r\n");
  14. fwrite($socket,"HOST: $remote_server\r\n");
  15. fwrite($socket,"Content-type: application/x-www-form-urlencoded\r\n");
  16. fwrite($socket,"Content-length: ".strlen($post_string)+8."\r\n");
  17. fwrite($socket,"Accept:*/*\r\n");
  18. fwrite($socket,"\r\n");
  19. fwrite($socket,"mypost=$post_string\r\n");
  20. fwrite($socket,"\r\n");

  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. $ch = curl_init();
  39. curl_setopt($ch,CURLOPT_URL,$remote_server);
  40. curl_setopt($ch,CURLOPT_POSTFIELDS,'mypost='.$post_string);
  41. curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);
  42. curl_setopt($ch,CURLOPT_USERAGENT,"Jimmy's CURL Example beta");
  43. $data = curl_exec($ch);
  44. curl_close($ch);
  45. return $data;
  46. }
  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. $context = array(
  55. 'http'=>array(
  56. ‘method’=>’POST’,
  57. ‘header’=>’Content-type: application/x-www-form-urlencoded’.”\r\n”.
  58. ‘User-Agent : Jimmy\’s POST Example beta’.”\r\n”.
  59. ‘Content-length: ‘.strlen($post_string)+8,
  60. ‘content’=>’mypost=’.$post_string)
  61. );
  62. $stream_context = stream_context_create($context);
  63. $data = file_get_contents($remote_server,FALSE,$stream_context);
  64. return $data;
  65. }

  66. ?>
复制代码




上一篇:curl伪造IP和来路
下一篇:扫描网站敏感目录的js脚本
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

1314学习网 ( 浙ICP备10214163号 )

GMT+8, 2025-5-2 10:54

Powered by Discuz! X3.4

© 2001-2013 Comsenz Inc.

快速回复 返回顶部 返回列表