PHP模拟post提交数据方法汇总


Posted in PHP onFebruary 16, 2016

使用php模拟post传值虽然在日常生活中用到的不是很多,但是在某些场合还是经常用到的。下面三水点靠木小编给大家整理了三种php模拟post传值的方法,file_get_contents、curl和socket。

第一种:file_get_contents来模拟post

<php

function file_get_contents_post($url, $post){
$options = array(
‘http‘=> array(
‘method‘=>‘POST‘,
‘content‘=> http_build_query($post),
),
);
$result = file_get_contents($url,false, stream_context_create($options));
return $result;
}
$data = file_get_contents_post("http://www.a.com/post/post.php", array(‘name‘=>‘caiknife‘,‘email‘=>‘caiknife#gmail.com‘));
var_dump($data);

第二种:curl模拟post

<php
function curl_post($url, $post){
$options = array(
CURLOPT_RETURNTRANSFER =>true,
CURLOPT_HEADER =>false,
CURLOPT_POST =>true,
CURLOPT_POSTFIELDS => $post,
);
$ch = curl_init($url);
curl_setopt_array($ch, $options);
$result = curl_exec($ch);
curl_close($ch);
return $result;
}
$data = curl_post("http://www.a.com/post/post.php", array(‘name‘=>‘caiknife‘,‘email‘=>‘caiknife#gmail.com‘));
var_dump($data);

第三种:socket来模拟post

<php
function socket_post($url, $post){
$urls = parse_url($url);
if(!isset($urls[‘port‘])){
$urls[‘port‘]=80;
}
$fp = fsockopen($urls[‘host‘], $urls[‘port‘], $errno, $errstr);
if(!$fp){
echo "$errno, $errstr";
exit();
}
$post = http_build_query($post);
$length = strlen($post);
$header =<<<HEADER
<span class="Apple-tab-span" style="white-space:pre"></span>POST {$urls[‘path‘]} HTTP/1.1
<span class="Apple-tab-span" style="white-space:pre"></span>Host:{$urls[‘host‘]}
<span class="Apple-tab-span" style="white-space:pre"></span>Content-Type: application/x-www-form-urlencoded
<span class="Apple-tab-span" style="white-space:pre"></span>Content-Length:{$length}
<span class="Apple-tab-span" style="white-space:pre"></span>Connection: close
<span class="Apple-tab-span" style="white-space:pre"></span>{$post}
<span class="Apple-tab-span" style="white-space:pre"></span>HEADER;
fwrite($fp, $header);
$result =‘‘;
while(!feof($fp)){
$result .= fread($fp,512);
}
$result = explode("\r\n\r\n", $result,2);
return $result[1];
}
$data = socket_post("http://www.a.com/post/post.php", array(‘name‘=>‘caiknife‘,‘email‘=>‘caiknife#gmail.com‘));
var_dump($data);

上面这三种方法最后看到的内容都是一样的,都可以得到post的传值;但是在是用socket的时候,发送header信息时必须要注意header的完整信息,比如content type和content length必须要有,connection: close和post数据之间要空一行,等等;而通过socket取得的内容是包含了header信息的,要处理一下才能获得真正的内容。

下面给大家说下php模拟post提交请求,调用接口

/**
* 模拟post进行url请求
* @param string $url
* @param string $param
*/
function request_post($url = '', $param = '') {
if (empty($url) || empty($param)) {
return false;
}
$postUrl = $url;
$curlPost = $param;
$ch = curl_init();//初始化curl
curl_setopt($ch, CURLOPT_URL,$postUrl);//抓取指定网页
curl_setopt($ch, CURLOPT_HEADER, 0);//设置header
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);//要求结果为字符串且输出到屏幕上
curl_setopt($ch, CURLOPT_POST, 1);//post提交方式
curl_setopt($ch, CURLOPT_POSTFIELDS, $curlPost);
$data = curl_exec($ch);//运行curl
curl_close($ch);
return $data;
}

这是方法,

下面是具体的调用案例。

function testAction(){
$url = 'http://mobile.jschina.com.cn/jschina/register.php';
$post_data['appid'] = '10';
$post_data['appkey'] = 'cmbohpffXVR03nIpkkQXaAA1Vf5nO4nQ';
$post_data['member_name'] = 'zsjs123';
$post_data['password'] = '123456';
$post_data['email'] = 'zsjs123@126.com';
$o = "";
foreach ( $post_data as $k => $v ) 
{ 
$o.= "$k=" . urlencode( $v ). "&" ;
}
$post_data = substr($o,0,-1);
$res = $this->request_post($url, $post_data); 
print_r($res);
}

这样就提交请求,并且获取请求结果了。一般返回的结果是json格式的。

这里的post是拼接出来的。

也可以改造成下面的方式。

/**
* 模拟post进行url请求
* @param string $url
* @param array $post_data
*/
function request_post($url = '', $post_data = array()) {
if (empty($url) || empty($post_data)) {
return false;
}
$o = "";
foreach ( $post_data as $k => $v ) 
{ 
$o.= "$k=" . urlencode( $v ). "&" ;
}
$post_data = substr($o,0,-1);
$postUrl = $url;
$curlPost = $post_data;
$ch = curl_init();//初始化curl
curl_setopt($ch, CURLOPT_URL,$postUrl);//抓取指定网页
curl_setopt($ch, CURLOPT_HEADER, 0);//设置header
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);//要求结果为字符串且输出到屏幕上
curl_setopt($ch, CURLOPT_POST, 1);//post提交方式
curl_setopt($ch, CURLOPT_POSTFIELDS, $curlPost);
$data = curl_exec($ch);//运行curl
curl_close($ch);
return $data;
}

将拼接也封装了起来,这样调用的时候就更简洁了。

function testAction(){
$url = 'http://mobile.jschina.com.cn/jschina/register.php';
$post_data['appid'] = '10';
$post_data['appkey'] = 'cmbohpffXVR03nIpkkQXaAA1Vf5nO4nQ';
$post_data['member_name'] = 'zsjs124';
$post_data['password'] = '123456';
$post_data['email'] = 'zsjs124@126.com';
//$post_data = array();
$res = $this->request_post($url, $post_data); 
print_r($res);
}
PHP 相关文章推荐
php 读取shell管道传输过来的内容
Mar 01 PHP
PHP中simplexml_load_string函数使用说明
Jan 01 PHP
php对包含html标签的字符串进行截取的函数分享
Jun 19 PHP
ThinkPHP后台首页index使用frameset时的注意事项分析
Aug 22 PHP
Yii2实现让关联字段支持搜索功能的方法
Aug 10 PHP
php实现文章置顶功能的方法
Oct 20 PHP
Laravel中服务提供者和门面模式的入门介绍
Nov 06 PHP
PHP通过bypass disable functions执行系统命令的方法汇总
May 02 PHP
Smarty模板变量与调节器实例详解
Jul 20 PHP
PHP Primary script unknown 解决方法总结
Aug 22 PHP
Laravel框架处理用户的请求操作详解
Dec 20 PHP
让whoops帮我们告别ThinkPHP6的异常页面
Mar 02 PHP
使用PHP处理数据库数据如何将数据返回客户端并显示当前状态
Feb 16 #PHP
PHP的Yii框架入门使用教程
Feb 15 #PHP
在Mac OS的PHP环境下安装配置MemCache的全过程解析
Feb 15 #PHP
ThinkPHP设置禁止百度等搜索引擎转码(简单实用)
Feb 15 #PHP
ECshop 迁移到 PHP7版本时遇到的兼容性问题
Feb 15 #PHP
PHP扩展迁移为PHP7扩展兼容性问题记录
Feb 15 #PHP
PHP+MySQL统计该库中每个表的记录数并按递减顺序排列的方法
Feb 15 #PHP
You might like
php visitFile()遍历指定文件夹函数
2010/08/21 PHP
round robin权重轮循算法php实现代码
2016/05/28 PHP
PHP自定义错误用法示例
2016/09/28 PHP
Yii2.0框架实现带分页的多条件搜索功能示例
2019/02/20 PHP
TP5(thinkPHP5)框架使用ajax实现与后台数据交互的方法小结
2020/02/10 PHP
javascript实现状态栏中文字动态显示的方法
2015/10/20 Javascript
关于JS中的apply,call,bind的深入解析
2016/04/05 Javascript
js遍历json的key和value的实例
2017/01/22 Javascript
js实现不提示直接关闭网页窗口
2017/03/30 Javascript
JavaScript脚本语言是什么_动力节点Java学院整理
2017/06/26 Javascript
zTree异步加载展开第一级节点的实现方法
2017/09/05 Javascript
详解webpack4多入口、多页面项目构建案例
2018/05/25 Javascript
vue实现element-ui对话框可拖拽功能
2018/08/17 Javascript
vue多级复杂列表展开/折叠及全选/分组全选实现
2018/11/05 Javascript
通过js示例讲解时间复杂度与空间复杂度
2019/08/06 Javascript
element-ui中Table表格省市区合并单元格的方法实现
2019/08/07 Javascript
Vue项目环境搭建详细总结
2019/09/26 Javascript
[07:26]2015国际邀请赛第二日TOP10集锦
2015/08/06 DOTA
[43:58]DOTA2-DPC中国联赛定级赛 LBZS vs SAG BO3第一场 1月8日
2021/03/11 DOTA
go和python调用其它程序并得到程序输出
2014/02/10 Python
Windows下Eclipse+PyDev配置Python+PyQt4开发环境
2016/05/17 Python
Python三种遍历文件目录的方法实例代码
2018/01/19 Python
神经网络(BP)算法Python实现及应用
2018/04/16 Python
使用Django启动命令行及执行脚本的方法
2018/05/29 Python
Python OpenCV对本地视频文件进行分帧保存的实例
2019/01/08 Python
彻底搞懂python 迭代器和生成器
2020/09/07 Python
Python获取android设备cpu和内存占用情况
2020/11/15 Python
HTML5实现经典坦克大战坦克乱走还能发出一个子弹
2013/09/02 HTML / CSS
远程研修随笔感言
2014/02/10 职场文书
工作求职自荐信
2014/06/13 职场文书
某集团股份有限公司委托书样本
2014/09/24 职场文书
建筑横幅标语
2014/10/09 职场文书
大学生档案自我鉴定(2篇)
2014/10/14 职场文书
《百分数的认识》教学反思
2016/02/19 职场文书
将MySQL的表数据全量导入clichhouse库中
2022/03/21 MySQL
docker-compose部署Yapi的方法
2022/04/08 Servers