php 模拟POST提交
//要post的数据
$argv = array('var1'=>'abc','var2'=>'你好吗');
//1、构造要post的字符串
foreach ($argv as $key=>$value)
{
$params.= $key."=".urlencode($value)."&";
}
//去除最后一个&
if($params)
{
$params = substr($params,0,-1);
}
$length = strlen($params);
//2、创建socket连接
$fp = fsockopen("127.0.0.1",80,$errno,$errstr,10) or exit($errstr."--->".$errno);
//3、构造post请求的头
$header = "POST /mobile/try.php HTTP/1.1\r\n";
$header .= "Host:127.0.0.1\r\n";
$header .= "Referer:/mobile/sendpost.php\r\n";
//post请求必须下面两行
$header .= "Content-Type: application/x-www-form-urlencoded\r\n";
$header .= "Content-Length: ".$length."\r\n";
$header .= "Connection: Close\r\n\r\n";
//4、添加post的字符串
$header .= $params."\r\n";
//5、发送post的数据
fputs($fp,$header);
$inheader = 1;
while (!feof($fp)) {
echo fgets($fp,1024);
}
fclose($fp);