分享使用php模拟POST提交的三种方法,可以做的事很多:提交表单,发布留言。稍稍改进一下可以当做 php留言群发的小工具使用,只公布两种方法,其它的自己加工吧。

关php于执行效率的问题,可以改进为linux底下命令行执行php,减少输出和增加多任务。

以下是代码:

1、通过curl函数

  1. $post_data =array();
  2. $post_data['clientname'] ="test08";
  3. $post_data['clientpasswd'] = "test08";
  4. $post_data['submit'] ="submit";
  5. $url='http://xxx.xxx.xxx.xx/xx/xxx/top.php';
  6. $o="";
  7. foreach ($post_dataas$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. //为了支持将输出结果curl_exec($ch)赋值给变量$result
  17. curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
  18. //为了支持cookie
  19. curl_setopt($ch,CURLOPT_COOKIEJAR,'cookie.txt');
  20. curl_setopt($ch,CURLOPT_POSTFIELDS,$post_data);
  21. $result = curl_exec($ch);

2.通过fsockopen

  1. $URL=‘http://xxx.xxx.xxx.xx/xx/xxx/top.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.  
  12. // making string from $data
  13. foreach($post_data as$key=>$value)
  14. $values[]="$key=".urlencode($value);
  15.  
  16. $data_string=implode("&",$values);
  17. // Find out which port is needed - if not given use standard (=80)
  18. if(!isset($URL_Info["port"]))
  19. $URL_Info["port"]=80;
  20. // building POST-request:
  21. $request.="POST".$URL_Info["path"]." HTTP/1.1\n";
  22. $request.="Host:".$URL_Info["host"]."\n";
  23. $request.="Referer:$referrer\n";
  24. $request.="Content-type: application/x-www-form-urlencoded\n";
  25. $request.="Content-length:".strlen($data_string)."\n";
  26. $request.="Connection: close\n";
  27. $request.="\n";
  28. $request.=$data_string."\n";
  29. $fp = fsockopen($URL_Info["host"],$URL_Info["port"]);
  30. fputs($fp,$request);
  31. while(!feof($fp)) {
  32.     $result .=fgets($fp,128);
  33. }
  34. fclose($fp);

3.使用httpclient类

  1. $pageContents =HttpClient::quickPost('http://example.com/someForm',array(       
  2.     'name' =>'Some Name',       
  3.     'email' =>'email@example.com'      
  4. ));

附:httpclient类库 下载 及 httpclient 类库的其它用法举例:

检查页面是否存在

  1. $client =new HttpClient('bankcha.com');       
  2. $client->setDebug(true);       
  3. if (!$client->get('/thispagedoesnotexist')){       
  4.     die('An error occurred:'.$client->getError());       
  5. }       
  6. if ($client->getStatus() == '404'){       
  7.     echo 'Page does not exist!';       
  8. }       
  9. $pageContents = $client->getContent();

伪造客户端

  1. $client =new HttpClient('bankcha.com');       
  2. $client->setDebug(true);       
  3. $client->setUserAgent('Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.3a) Gecko/20021207');       
  4. if (!$client->get('/')){       
  5.     die('An error occurred:'.$client->getError());       
  6. }       
  7. $pageContents = $client->getContent();

登录验证并请求一个网页

  1. $client =new HttpClient('bankcha.com');       
  2. $client->post('/login.php',array(       
  3.     'username' =>'Simon',       
  4.     'password' =>'ducks'      
  5. ));       
  6. if (!$client->get('/private.php')){       
  7.     die('An error occurred:'.$client->getError());       
  8. }       
  9. $pageContents = $client->getContent();

httpclient类 更多用法实例,可参考:
http://scripts.incutio.com/httpclient/index.php




 

Logo

更多推荐