php接收post请求,php 模拟post请求,并且接收POST数据后进行处理

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);

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值