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提交数据的更多相关文章

  1. 三种方法教你HTML实现点击某一个元素之外触发事件

    HTML实现点击某一个元素之外触发事件 大致编写的HTML界面渲染后是这个样子的,我们现在想要实现的需求是点击Button所在的div不会触发事件,而在点击Button所在的div之外的区域时会触发事 ...

  2. 电视不支持AirPlay镜像怎么办?苹果iPhone手机投屏三种方法

    导读:苹果手机多屏互动功能在哪里?iPhone苹果手机没有AirPlay镜像怎么办?三种方法教你苹果iPhone手机怎么投影到智能电视上. 前言: 苹果iPhone手机投屏到电视设备上,需要使用到Ai ...

  3. ASP.NET MVC 中将数据从View传递到控制器中的三种方法(表单数据绑定)

    http://www.cnblogs.com/zyqgold/archive/2010/11/22/1884779.html 在ASP.NET MVC框架中,将视图中的数据传递到控制器中,主要通过发送 ...

  4. Javascript定义类(class)的三种方法

    将近20年前,Javascript诞生的时候,只是一种简单的网页脚本语言.如果你忘了填写用户名,它就跳出一个警告. 如今,它变得几乎无所不能,从前端到后端,有着各种匪夷所思的用途.程序员用它完成越来越 ...

  5. php生成随机数的三种方法

    php生成随机数的三种方法 如何用php生成1-10之间的不重复随机数? 例1,使用shuffle函数生成随机数. <?php$arr=range(1,10);shuffle($arr);for ...

  6. [转]Javascript定义类的三种方法

    作者: 阮一峰 原文地址:http://www.ruanyifeng.com/blog/2012/07/three_ways_to_define_a_javascript_class.html 将近2 ...

  7. Javascript 创建对象的三种方法及比较【转载+整理】

    https://developer.mozilla.org/zh-CN/docs/JavaScript/Guide/Inheritance_and_the_prototype_chain 本文内容 引 ...

  8. 服务器文档下载zip格式 SQL Server SQL分页查询 C#过滤html标签 EF 延时加载与死锁 在JS方法中返回多个值的三种方法(转载) IEnumerable,ICollection,IList接口问题 不吹不擂,你想要的Python面试都在这里了【315+道题】 基于mvc三层架构和ajax技术实现最简单的文件上传 事件管理

    服务器文档下载zip格式   刚好这次项目中遇到了这个东西,就来弄一下,挺简单的,但是前台调用的时候弄错了,浪费了大半天的时间,本人也是菜鸟一枚.开始吧.(MVC的) @using Rattan.Co ...

  9. Iterator to list的三种方法

    目录 简介 使用while 使用ForEachRemaining 使用stream 总结 Iterator to list的三种方法 简介 集合的变量少不了使用Iterator,从集合Iterator ...

随机推荐

  1. js事件驱动函数

    输入框 获得光标的这个行为叫做获取焦点 失去光标的这个行为叫做失去焦点 blur 失去焦点 1.获取标签的时候,一定要先等页面加载完成,再去获取这个标签. 可以将整个script代码写在body的下面 ...

  2. ***腾讯云直播(含微信小程序直播)研究资料汇总-原创

    这段时间抽空研究了下直播技术,综合比较了下腾讯云直播的技术和文档方面最齐全,现把一些技术资料和文档归集如下: 1.微信小程序移动直播入门导读 https://cloud.tencent.com/doc ...

  3. Android动态设置纯色图标的颜色

    https://blog.csdn.net/qq_20082961/article/details/73718363 以前做了一个流量悬浮窗,悬浮窗里有当前网络状态的图标和网速的文字,想实现改变文字颜 ...

  4. 接口隔离原则(Interface Segregation Principle, ISP)

    使用多个专门的接口,而不使用单一的总接口 接口隔离有两种定义: Clients should not be forced to depend upon interfaces that they don ...

  5. Python4 - 文件操作

    对文件操作流程 打开文件,得到文件句柄并赋值给一个变量 文件的内存对象-包含 文件名.字符集.大小.在硬盘上的起止位置... 通过句柄对文件进行操作 关闭文件 open 方法 open()函数打开一个 ...

  6. RocketMQ msgId生成算法

    当我们用RocketMQ发送信息的时候通常都会返回如下信息: SendResult [sendStatus=SEND_OK, msgId=0A42333A0DC818B4AAC246C290FD000 ...

  7. python tkinter-单选、多选

      单选按钮 tkinter.Radiobutton(root,text='a').pack() tkinter.Radiobutton(root,text='b').pack() tkinter.R ...

  8. macOS下通过docker在局域网成功访问mysql5.6数据库

    1.获取mysql镜像 docker pull mysql:5.6 注意:此处之所以获取mysql5.6是因为mysql5.7在centos7中启动可能会报错, 2.查看镜像列表 docker ima ...

  9. UVa140 Bandwidth 【最优性剪枝】

    题目链接:https://vjudge.net/contest/210334#problem/F  转载于:https://www.cnblogs.com/luruiyuan/p/5847706.ht ...

  10. POJ 2337 Catenyms(有向欧拉图:输出欧拉路径)

    题目链接>>>>>> 题目大意: 给出一些字符串,问能否将这些字符串  按照 词语接龙,首尾相接  的规则 使得每个字符串出现一次 如果可以 按字典序输出这个字符串 ...