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 ...
随机推荐
- lua 工具类(二)
local tonumber_ = tonumber function tonumber(v, base) end function toint(v) return math.round(tonumb ...
- jsonrpc使用
jsonrpc使用 1.什么是rpc RPC(Remote Procedure Call)远程过程调用,简单说就是通过网络请求服务,不需要了解底层网络技术的协议. 常用语分布式应用程序. 2.rpc数 ...
- DIOCP开源项目-数据库连接池的使用<多帐套数据库>
很久没有写DIOCP的Demo了,主要公司的事情太繁琐,工作之余都不想动了,之前承若的群里面朋友们的DEMO,昨天晚上恶补了一下,把对数据库连接池的操作加入到了Demo中,大家可以通过SVN下载到最新 ...
- spring boot guava cache 缓存学习
http://blog.csdn.net/hy245120020/article/details/78065676 ****************************************** ...
- type、object和class的关系
- idea 实现热部署
Idea实现热部署需要以下几步 1.open module setting,设置path,使output path指向存放实时编译class的文件夹 2.设置Artifacts,选择exploded选 ...
- postgresql相关命令
1,打开命令窗口: 2,查看数据库用户:\du 3,列出所有数据库名:\l或者SELECT datname FROM pg_database; 4,切换某个数据库下面的某个用户下面:\c 数据库名 用 ...
- JAVA-JSP内置对象
相关资料:<21天学通Java Web开发> request 请求对象 类型javax.servlet.ServletRequest 作用域Requestresponse 响应对象 类型j ...
- Android Retrofit2 网路编程
Android里面本身有OKHttp,不过不是很好用,这里就用Retrofit2,简单好用. 首先,需要加入网络权限: <uses-permission android:name="a ...
- velocity单引号与双引号
(1)最外层是用单引号包围时,双引号直接使用就可以了,两个连续的单引号表示一个单引号:#set($var2 = 'A"B''C') --> $var2 的值为 A"B'C(2 ...