Discuz教程网

php 模拟POST提交的2种方法

[复制链接]
authicon dly 发表于 2011-1-7 12:35:46 | 显示全部楼层 |阅读模式
1.通过curl函数
  1. $post_data = array();
  2. $post_data[\'clientname\'] = "test08";
  3. $post_data[\'clientpasswd\'] = "test08";
  4. $post_data[\'submit\'] = "submit";
  5. $url=\'http://yourdomain/test.php\';
  6. $o="";
  7. foreach ($post_data as $k=>$v)
  8. {
  9.     $o.= "$k=".urlencode($v)."&";
  10. }
  11. $post_data=substr($o,0,-1);
  12. $ch = curl_init();
  13. curl_setopt($ch, CURLOPT_POST, 1);
  14. curl_setopt($ch, CURLOPT_HEADER, 0);
  15. curl_setopt($ch, CURLOPT_URL,$url);
  16. //为了支持cookie
  17. curl_setopt($ch, CURLOPT_COOKIEJAR, \'cookie.txt\');
  18. curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
  19. $result = curl_exec($ch);
复制代码
2.通过fsockopen.
  1. $URL=‘http://yourdomain/test.php\';
  2. $post_data[\'clientname\'] = "test08";
  3. $post_data[\'clientpasswd\'] = "test08";
  4. $post_data[\'submit\'] = "ログイン";
  5. $referrer="";
  6. // parsing the given URL
  7. $URL_Info=parse_url($URL);
  8. // Building referrer
  9. if($referrer=="") // if not given use this script as referrer
  10. $referrer=$_SERVER["SCRIPT_URI"];

  11. // making string from $data
  12. foreach($post_data as $key=>$value)
  13. $values[]="$key=".urlencode($value);

  14. $data_string=implode("&",$values);
  15. // Find out which port is needed - if not given use standard (=80)
  16. if(!isset($URL_Info["port"]))
  17. $URL_Info["port"]=80;
  18. // building POST-request:
  19. $request.="OST ".$URL_Info["path"]." HTTP/1.1\\n";
  20. $request.="Host: ".$URL_Info["host"]."\\n";
  21. $request.="Referer: $referrer\\n";
  22. $request.="Content-type: application/x-www-form-urlencoded\\n";
  23. $request.="Content-length: ".strlen($data_string)."\\n";
  24. $request.="Connection: close\\n";
  25. $request.="\\n";
  26. $request.=$data_string."\\n";
  27. $fp = fsockopen($URL_Info["host"],$URL_Info["port"]);
  28. fputs($fp, $request);
  29. while(!feof($fp)) {
  30.     $result .= fgets($fp, 128);
  31. }
  32. fclose($fp);
复制代码







上一篇:PHP IP地址和十进制数字互转
下一篇:PHP的ASCII码转换类
authicon 皆无 发表于 2011-6-19 09:00:30 | 显示全部楼层
楼主真强大
authicon lightning123 发表于 2011-6-19 23:00:21 | 显示全部楼层
这个还是不错的!
authicon 咫尺天 发表于 2011-6-27 12:59:32 | 显示全部楼层
这个还是不错的!
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

1314学习网 ( 浙ICP备10214163号 )

GMT+8, 2025-5-3 01:28

Powered by Discuz! X3.4

© 2001-2013 Comsenz Inc.

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