萬盛學電腦網

 萬盛學電腦網 >> 網絡編程 >> php編程 >> 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.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);

  }

copyright © 萬盛學電腦網 all rights reserved