如何用PHP模拟POST方法 采集提交后的数据
来源:互联网 发布:淘宝类似的购物网站 编辑:程序博客网 时间:2024/04/30 20:47
它需要得到POST到目标网站之后,目标网站分析POST值之后输出的HTML页面。。恩,上网找了很久,走了很多弯路,现在把经验拿出来分享,希望各位以后少走弯路。
用PHP实现模拟POST传值并得到传值之后的页面,核心是fsockopen()函数.它模拟HTTP协议传送$_POST数组到目标网站,并循环得到目标网站的字符串!以下是例子 大家可以试试看。<?PHP
$flag = 0;
//要post的数据
$argv = array(
'var1'=>'abc',
'var2'=>'你好吗');
//构造要post的字符串
foreach ($argv as $key=>$value) {
if ($flag!=0) {
$params .= "&";
$flag = 1;
}
$params.= $key."="; $params.= urlencode($value);
$flag = 1;
}
$length = strlen($params);
//创建socket连接
$fp = fsockopen("127.0.0.1",80,$errno,$errstr,10) or exit($errstr."--->".$errno);
//构造post请求的头
$header = "POST /mobile/try.php HTTP/1.1\r\n";
$header .= "Host:127.0.0.1\r\n";
$header .= "Referer:/mobile/sendpost.php\r\n";
$header .= "Content-Type: application/x-www-form-urlencoded\r\n";
$header .= "Content-Length: ".$length."\r\n";
$header .= "Connection: Close\r\n\r\n";
//添加post的字符串
$header .= $params."\r\n";
//发送post的数据
fputs($fp,$header);
$inheader = 1;
while (!feof($fp)) {
$line = fgets($fp,1024); //去除请求包的头只显示页面的返回数据
if ($inheader && ($line == "\n" || $line == "\r\n")) {
$inheader = 0;
}
if ($inheader == 0) {
echo $line;
}
}
fclose($fp);
?>
采用80端口是因为浏览器默认正常访问是80端口嘛,如果目标网站访问端口不同,大家就需要改端口了,恩,其实很简单的。
GET行为比较简单,POST比较复杂一些。这里提供两种方法供选择:第一:手写代码。第二:利用HttpClient php类库
第二种方法是:使用httpclient类
PHP代码
$pageContents = HttpClient::quickPost('http://example.com/someForm', array(
'name' => 'Some Name',
'email' => 'email@example.com'
));
使用httpclient类库,可以去官方下载最新的类库,官方地址为:http://scripts.incutio.com/httpclient/index.php
附加一些点php httpclient的其他几个用法
静态方法获取网页:
PHP代码
$pageContents = HttpClient::quickGet('http://bankcha.com')
Get方法获取
PHP代码
$client = new HttpClient('bankcha.com');
if (!$client->get('/')) {
die('An error occurred: '.$client->getError());
}
$pageContents = $client->getContent();
带调试的Get方法获取
PHP代码
$client = new HttpClient('bankcha.com');
$client->setDebug(true);
if (!$client->get('/')) {
die('An error occurred: '.$client->getError());
}
$pageContents = $client->getContent();
带自动转向的Get方法
PHP代码
$client = new HttpClient('www.bankcha.com');
$client->setDebug(true);
if (!$client->get('/')) {
die('An error occurred: '.$client->getError());
}
$pageContents = $client->getContent();
检查页面是否存在
PHP代码
$client = new HttpClient('bankcha.com');
$client->setDebug(true);
if (!$client->get('/thispagedoesnotexist')) {
die('An error occurred: '.$client->getError());
}
if ($client->getStatus() == '404') {
echo 'Page does not exist!';
}
$pageContents = $client->getContent();
伪造客户端
PHP代码
$client = new HttpClient('bankcha.com');
$client->setDebug(true);
$client->setUserAgent('Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.3a) Gecko/20021207');
if (!$client->get('/')) {
die('An error occurred: '.$client->getError());
}
$pageContents = $client->getContent();
登录验证并请求一个网页
PHP代码
$client = new HttpClient('bankcha.com');
$client->post('/login.php', array(
'username' => 'Simon',
'password' => 'ducks'
));
if (!$client->get('/private.php')) {
die('An error occurred: '.$client->getError());
}
$pageContents = $client->getContent();
HTTP授权
PHP代码
$client = new HttpClient('bankcha.com');
$client->setAuthorization('Username', 'Password');
if (!$client->get('/')) {
die('An error occurred: '.$client->getError());
}
$pageContents = $client->getContent();
输出头信息
PHP代码
$client = new HttpClient('bankcha.com');
if (!$client->get('/')) {
die('An error occurred: '.$client->getError());
}
print_r($client->getHeaders());
设置一个域内重定向最多次数
PHP代码
$client = new HttpClient('www.bankcha.com');
$client->setDebug(true);
$client->setMaxRedirects(3);
$client->get('/');
- 如何用PHP模拟POST方法 采集提交后的数据
- php模拟post提交数据方法
- PHP中模拟post提交数据的方法
- php 模拟POST提交数据的2种方法
- php模拟post提交数据,用处很多,可用来网站的采集,登陆等等
- 如何用chrome查看提交的post表单数据
- php 开启curl扩展 后 模拟post提交数据
- ASP模拟POST提交数据的方法
- php模拟post提交数据
- php模拟post提交数据
- php模拟post提交数据
- php模拟post提交数据
- php模拟post提交提交json数据
- php中模拟POST提交数据两种方法
- PHP利用CURL方法模拟POST提交数据并获得返回值的小细节
- 在vc中如何用post方法提交表单
- php curl模拟post提交数据的例子
- php 模拟POST提交的2种方法
- 如何把一个java程序打包成exe文件,运行在没有java虚
- CodeWarrior下载程序到9S12XS128
- Eclipe 工具插件一栏
- Xcode 中设置部分文件ARC支持
- vim的配置文件
- 如何用PHP模拟POST方法 采集提交后的数据
- IT运维管理系统软件标准性定位
- linux 面试题
- ARM的启动分析
- 【转】一个程序员分享8年的开发经验
- 刘同——谁的青春不迷茫,给你冬日满满正能量
- X86单网卡Desktop源码安装Openstack 组件之Keystone、Glance、Nova
- 逻辑卷管理
- HBase之二【HBase基础】hbase介绍(2)