php模拟post提交数据,用处很多,可用来网站的采集,登陆等等
1. [代码][PHP]代码
<?php //以程序登陆一个论坛登录为例
function bbslogin($user_login, $password, $host, $port = "80") {
//需要提交的post数据
$argv = array('cookie' => array('user_login' => $user_login, 'password' => $password, '_wp_http_referer' => '/bbpress/', 're' => '', 'remember' => true));
foreach ($argv['cookie'] as $key => $value) {
$params[] = $key . '=' . $value;
}
$params = implode('&', $params);
$header = "POST /bbpress/bb-login.php HTTP/1.1\r\n";
$header .= "Host:$host:$port\r\n";
$header .= "Content-Type: application/x-www-form-urlencoded\r\n";
$header .= "Content-Length: " . strlen($params) . "\r\n";
$header .= "Connection: Close\r\n\r\n";
$header .= $params;
$fp = fsockopen($host, $port);
fputs($fp, $header);
while (!feof($fp)) {
$str = fgets($fp);
//以下是自己的逻辑代码,这里主要是模拟cookie,可用来同步登陆
if (!(strpos($str, "Set-Cookie:") === false)) {
$tmparray = explode(" ", $str);
$cookiearray = explode("=", $tmparray[1]);
$cookiepaths = explode("=", $tmparray[6]);
$cookiename = urldecode($cookiearray[0]);
$cookievalue = urldecode(substr($cookiearray[1], 0, strlen($cookiearray[1]) - 1));
$cookietime = time() + 3600 * 24 * 7;
$cookiepath = urldecode(substr($cookiepaths[1], 0, strlen($cookiepaths[1]) - 1));
setcookie($cookiename, $cookievalue, $cookietime, $cookiepath);
}
}
fclose($fp);
}
?>
2. [代码][PHP]代码
<?php
// PHP POST数据的三种方法
// php有三种方法可以post数据,分别为Curl、socket、file_get_contents: /**
* Socket版本
* 使用方法:
* $post_string = "app=socket&version=beta";
* request_by_socket('facebook.cn','/restServer.php',$post_string);
*/
function request_by_socket($remote_server, $remote_path, $post_string, $port = 80, $timeout = 30)
{
$socket = fsockopen($remote_server, $port, $errno, $errstr, $timeout);
if (!$socket) die("$errstr($errno)"); fwrite($socket, "POST $remote_path HTTP/1.0\r\n");
fwrite($socket, "User-Agent: Socket Example\r\n");
fwrite($socket, "HOST: $remote_server\r\n");
fwrite($socket, "Content-type: application/x-www-form-urlencoded\r\n");
fwrite($socket, "Content-length: " . (strlen($post_string) + 8) . '\r\n');
fwrite($socket, "Accept:*/*\r\n");
fwrite($socket, "\r\n");
fwrite($socket, "mypost=$post_string\r\n");
fwrite($socket, "\r\n");
$header = "";
while ($str = trim(fgets($socket, 4096))) {
$header .= $str;
}
$data = "";
while (!feof($socket)) {
$data .= fgets($socket, 4096);
}
return $data;
} /**
* Curl版本
* 使用方法:
* $post_string = "app=request&version=beta";
* request_by_curl('http://facebook.cn/restServer.php',$post_string);
*/
function request_by_curl($remote_server, $post_string)
{
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $remote_server);
curl_setopt($ch, CURLOPT_POSTFIELDS, 'mypost=' . $post_string);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_USERAGENT, "Jimmy's CURL Example beta");
$data = curl_exec($ch);
curl_close($ch);
return $data;
} /**
* 其它版本
* 使用方法:
* $post_string = "app=request&version=beta";
* request_by_other('http://facebook.cn/restServer.php',$post_string);
*/
function request_by_other($remote_server, $post_string)
{
$context = array(
'http' => array(
'method' => 'POST',
'header' => 'Content-type: application/x-www-form-urlencoded' .
'\r\n'.'User-Agent : Jimmy\'s POST Example beta' .
'\r\n'.'Content-length:' . strlen($post_string) + 8,
'content' => 'mypost=' . $post_string)
);
$stream_context = stream_context_create($context);
$data = file_get_contents($remote_server, false, $stream_context);
return $data;
} ?>
php模拟post提交数据,用处很多,可用来网站的采集,登陆等等的更多相关文章
- cURL模拟POST提交数据
首先,是这个代码: <?php //curl模拟post提交数据$url = "http://127.0.0.1/immoc/output.php";$post_data = ...
- 使用PHP模拟post提交数据
使用PHP模拟post提交数据 分类: PHP LAMP 2013-04-13 12:03 3954人阅读 评论(0) 收藏 举报 CurlsocketPHP 这也是个老生常谈的话题了,上午花了点时间 ...
- Fiddler进行模拟Post提交数据,总为null解决方式
Fiddler模拟post提交时总是为空,解决办法 如果是表单提交则要在header加上 ContentType:application/x-www-form-urlencoded 如果是要post提 ...
- php CURL 模拟 POST 提交数据
<?php function liansuo_post($url,$data){ // 模拟提交数据函数 $curl = curl_init(); // 启动一个CURL会话 curl_seto ...
- delphi 模拟POST提交数据
unit GetHttpInfo; interface uses Classes, WinINet, Sysutils, windows, IDURI, IdSSLOpenSSL , IdBaseCo ...
- 三种方法教你如何用PHP模拟post提交数据
php模拟post传值在日常的工作中用到的不是很多,但是在某些特定的场合还是经常用到的. 下面,我整理了三种php模拟post传值的方法,file_get_contents.curl和socket. ...
- 模拟form提交数据
最近在做一个项目,发现ajax不能enctype=”multipart/form-data” 属性的表单,没办法,只能使用form表单直接提交的方法了,但是form表单直接提交会跳转页面,这样很不友好 ...
- Asp.Net模拟post提交数据方法
方法1: System.Net.WebClient WebClientObj = new System.Net.WebClient(); System.Collections.Specialized. ...
- php模拟post提交数据
$data = '{ "id": "17999030", "method": "sayHello", "jso ...
随机推荐
- windows远程连接mac配置方法
1.开启mac共享服务,设置通过密码进行连接 [系统偏好设置]中选择[共享]打开[屏幕共享]服务,即可允许其他电脑的用户远程查看并控制此电脑.点击[电脑设置]配置访问密码 2. 下载TightVNC, ...
- mysql_secure_installation
安装完mysql-server 会提示可以运行mysql_secure_installation.运行mysql_secure_installation会执行几个设置: a)为root用户设置密码 ...
- MyBatis 问题列表
问题表现:The content of elements must consist of well-formed character data or markup 解决办法:1.配置的动态SQL语句里 ...
- RAID卡的缓存与磁盘自带的缓存的关系
RAID卡是否有(启用)缓存对“随机读写”性能有巨大的影响.中高端的RAID卡都有缓存(价格也高). 那么RAID卡的缓存与磁盘自带的缓存是如何设置的? 戴尔服务器的perc H710 RAID卡有5 ...
- python版本坑:md5例子(python2与python3中md5区别)
对于一些字符,python2和python3的md5加密出来是不一样的. Python2 和Python3MD5加密 # python2.7 pwd = "xxx" + chr(1 ...
- Django项目关闭debug模式后,静态文件无法加载的解决办法
开启内置服务器,由于项目中local_settings.py文件中的DEBUG=True,进行开发和调试一直没什么问题. 但是现在需要编写404,500等出错页面,在debug模式下出了错都会出现报错 ...
- 【Python】打印object对象
print (object .__dict__) print (dir(object))
- shell数组应用
引言 在Linux平台上工作,我们经常需要使用shell来编写一些有用.有意义的脚本程序.有时,会经常使用shell数组.那么,shell中的数组是怎么表现的呢,又是怎么定义的呢?接下来逐一的进行讲解 ...
- Oracle 安装报错 [INS-06101] IP address of localhost could not be determined 解决方法
安装Oracle 11gR2,报错:[INS-06101] IP address of localhost could not be determined 出现这种错误是因为主机名和/etc/host ...
- java基础篇---内存分析
Java的并发采用的是共享内存模型(而非消息传递模型),线程之间共享程序的公共状态,线程之间通过写-读内存中的公共状态来隐式进行通信.多个线程之间是不能直接传递数据交互的,它们之间的交互只能通过共享变 ...