php模拟post提交数据的方法,php模拟post提交_PHP教程
程序员文章站
2022-06-07 22:38:12
...
php模拟post提交数据的方法,php模拟post提交
本文实例讲述了php模拟post提交数据的方法。分享给大家供大家参考。具体如下:
php模拟post提交数据,用处很多,可用来网站的采集,登陆等等
这里以我项目中的论坛登录为例加以说明:
复制代码 代码如下:
function A_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);
}
//需要提交的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);
}
希望本文所述对大家的php程序设计有所帮助。
推荐阅读
-
PHP的cURL库功能简介:抓取网页,POST数据及其他_PHP教程
-
启用Csrf后POST数据时出现的400错误,csrfpost_PHP教程
-
php curl模拟post请求的例子
-
PHP使用stream_context_create()模拟POST/GET请求的方法,streamcontext_PHP教程
-
Zend Framework教程之Zend_Form组件实现表单提交并显示错误提示的方法_php实例
-
使用php curl模拟post请求的小例子。,curlpost_PHP教程
-
PHP利用socket模拟post之fsockopen发送数据_PHP教程
-
PHP之fsockopen提交POST数据讲解
-
解析curl提交GET,POST,Cookie的简单方法_PHP
-
关于PHP接收不到表单提交的POST信息