码迷,mamicode.com
首页 > Web开发 > 详细

三种方法教你如何用PHP模拟post提交数据

时间:2016-02-16 10:06:35      阅读:247      评论:0      收藏:0      [点我收藏+]

标签:

php模拟post传值在日常的工作中用到的不是很多,但是在某些特定的场合还是经常用到的。

下面,我整理了三种php模拟post传值的方法,file_get_contents、curl和socket。
?
第一种:file_get_contents来模拟post
?
  1. <?php
  2. ?
  3. function file_get_contents_post($url, $post){
  4. ?
  5. $options = array(
  6. ‘http‘=> array(
  7. ‘method‘=>‘POST‘,
  8. ‘content‘=> http_build_query($post),
  9. ),
  10. );
  11. ?
  12. $result = file_get_contents($url,false, stream_context_create($options));
  13. return $result;
  14. ?
  15. }
  16. ?
  17. $data = file_get_contents_post("http://www.a.com/post/post.php", array(‘name‘=>‘caiknife‘,‘email‘=>‘caiknife#gmail.com‘));
  18. var_dump($data);
?
第二种:curl模拟post
?
  1. <?php
  2. ?
  3. function curl_post($url, $post){
  4. ?
  5. $options = array(
  6. CURLOPT_RETURNTRANSFER =>true,
  7. CURLOPT_HEADER =>false,
  8. CURLOPT_POST =>true,
  9. CURLOPT_POSTFIELDS => $post,
  10. );
  11. ?
  12. ?
  13. $ch = curl_init($url);
  14. curl_setopt_array($ch, $options);
  15. $result = curl_exec($ch);
  16. curl_close($ch);
  17. return $result;
  18. }
  19. ?
  20. $data = curl_post("http://www.a.com/post/post.php", array(‘name‘=>‘caiknife‘,‘email‘=>‘caiknife#gmail.com‘));
  21. ?
  22. var_dump($data);
?
第三种:socket来模拟post
?
  1. <?php
  2. ?
  3. function socket_post($url, $post){
  4. $urls = parse_url($url);
  5. if(!isset($urls[‘port‘])){
  6. $urls[‘port‘]=80;
  7. }
  8. ?
  9. $fp = fsockopen($urls[‘host‘], $urls[‘port‘], $errno, $errstr);
  10. if(!$fp){
  11. echo "$errno, $errstr";
  12. exit();
  13. }
  14. ?
  15. $post = http_build_query($post);
  16. $length = strlen($post);
  17. $header =<<<HEADER
  18. ?
  19. <span class="Apple-tab-span" style="white-space:pre"></span>POST {$urls[‘path‘]} HTTP/1.1
  20. <span class="Apple-tab-span" style="white-space:pre"></span>Host:{$urls[‘host‘]}
  21. <span class="Apple-tab-span" style="white-space:pre"></span>Content-Type: application/x-www-form-urlencoded
  22. <span class="Apple-tab-span" style="white-space:pre"></span>Content-Length:{$length}
  23. <span class="Apple-tab-span" style="white-space:pre"></span>Connection: close
  24. <span class="Apple-tab-span" style="white-space:pre"></span>{$post}
  25. <span class="Apple-tab-span" style="white-space:pre"></span>HEADER;
  26. ?
  27. fwrite($fp, $header);
  28. $result =‘‘;
  29. while(!feof($fp)){
  30. $result .= fread($fp,512);
  31. }
  32. $result = explode("\r\n\r\n", $result,2);
  33. return $result[1];
  34. ?
  35. }
  36. ?
  37. $data = socket_post("http://www.a.com/post/post.php", array(‘name‘=>‘caiknife‘,‘email‘=>‘caiknife#gmail.com‘));
  38. var_dump($data);
?
上面这三种方法最后看到的内容都是一样的,都可以得到post的传值;但是在是用socket的时候,发送header信息时必须要注意header的完整信息,比如content type和content length必须要有,connection: close和post数据之间要空一行,等等;而通过socket取得的内容是包含了header信息的,要处理一下才能获得真正的内容。
?
http://www.shuchengxian.com/html/PHP/201512/23.html

三种方法教你如何用PHP模拟post提交数据

标签:

原文地址:http://www.cnblogs.com/meetrice/p/5191821.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!