萬盛學電腦網

 萬盛學電腦網 >> 網絡編程 >> php編程 >> php模擬post提交數據的方法

php模擬post提交數據的方法

 這篇文章主要介紹了php模擬post提交數據的方法,實例分析了socket方法模擬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.1rn";
$header .= "Host:$host:$portrn";
$header .= "Content-Type: application/x-www-form-urlencodedrn";
$header .= "Content-Length: " . strlen($params) . "rn";
$header .= "Connection: Closernrn";
$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程序設計有所幫助。

copyright © 萬盛學電腦網 all rights reserved