post.php
function post($url, $post){
$url = parse_url($url);
$fp = fsockopen($url['host'], $url['port'] ?
$url['port'] : 80, $errno, $errstr, 10);
if (!$fp) return "Failed to open socket to
$url[host]";
fputs($fp, sprintf("POST %s%s%s HTTP/1.1\n",
$url['path'], $url['query'] ? "?" : "", $url['query']));
fputs($fp, "Host: $url[host]\n");
fputs($fp, "Content-type:
application/x-www-form-urlencoded\n");
fputs($fp, "Content-length: " . strlen($post) .
"\n");
fputs($fp, "Cookie:
PHPSESSID=111111111111111111111111\r\n");
fputs($fp, "Connection: close\n\n");
fputs($fp, "$post \n");
while (!feof($fp)) {
echo fgets($fp, 128);
}
fclose($fp);
}
function t_post($url,$content,&$response) {
$content_length = strlen($content);
$options = array(
'http'=>array(
'method' =>
'POST',
'header'
=>
"Content-type:
application/x-www-form-urlencoded\r\n" .
"Command-length:
$content_length\r\n" .
"Content-length:
$content_length\r\n",
'content' =>
$content
)
);
$context = stream_context_create($options);
//$response = file_get_contents($url, false,
$context);
$fp = fopen($url, 'r', false, $context);
fpassthru($fp);
exit;
//return $response;
//if(strstr($response,"RES=")) return true;
//return false;
}
$url = "http://localhost/BAK/p_test.php";
$str =
"CMD=upload_web&FILE_NAME=aa";
//post($url,$str);
$ret= t_post($url, $str,$res);
echo $ret;
exit;
?>
p_test.php
function micat_parse_str($str,&$ar)
{
$t=explode("&",$str);
foreach($t as $item){
list($key,$val)=explode("=",$item);
if(empty($ar[$key])){
$ar[$key]=$val;
}else{
if(is_array($ar[$key])){
$ar[$key][]=$val;
}else{
$tmp=array();
$tmp[]=$ar[$key];
$tmp[]=$val;
$ar[$key]=$tmp;
}
}
}
}
$raw_post=file_get_contents("php://input");
$len= $_SERVER["HTTP_COMMAND_LENGTH"];
$par=array();
micat_parse_str($raw_post,$par);
print_r($par);
?>
注:fpassthru() 函数输出文件指针处的所有剩余数据。
该函数将给定的文件指针从当前的位置读取到 EOF,并把结果写到输出缓冲区。
$file = fopen("test.txt","r");
// 读取第一行
fgets($file);
// 把文件的其余部分发送到输出缓存
fpassthru($file);
fclose($file);