带有header()和错误header问题的PHP POST

带有header()和错误header问题的PHP POST,php,http,Php,Http,我遇到了一个在PHP中发送头的令人目瞪口呆的问题。我花了大约45分钟在SO和其他网站上阅读,我想不出一个合理的理由来解释我的问题 我需要向另一台服务器发送POST请求,我正在使用PHP header()函数设置值。我有下面的示例代码 $server = 'http://fakedomain.com'; $server_path = '/'; $request = 'key=value&key2=value2'; header("POST $server_p

我遇到了一个在PHP中发送头的令人目瞪口呆的问题。我花了大约45分钟在SO和其他网站上阅读,我想不出一个合理的理由来解释我的问题

我需要向另一台服务器发送POST请求,我正在使用PHP header()函数设置值。我有下面的示例代码

    $server = 'http://fakedomain.com';
    $server_path = '/';
    $request = 'key=value&key2=value2';
    header("POST $server_path HTTP/1.1" );
    header("Host: $server\r\n" );
    header("Content-type: application/x-www-form-urlencoded\r\n" );
    header("Content-length: ".strlen($request)."\r\n" );
    header("Connection: close\r\n\r\n" );
    header($request);
我尝试了多种选择,但每种选择都会导致日志文件中出现相同的错误

malformed header from script. Bad header=POST / HTTP/1.1: php5.cgi
我是一名经验丰富的PHP程序员,只是不太需要手动发送HTTP post请求

我希望代码重定向浏览器,这就是我决定使用此方法的原因

我做得对吗?

是否有其他标准方式,我只是不知道?header()发送响应头

听起来你想在后端发出请求

因此,您可能希望使用来发出请求

如果在处理完响应后,希望向用户代理(浏览器)发送某种头,则header()是合适的。

header()发送响应头

听起来你想在后端发出请求

因此,您可能希望使用来发出请求


如果在处理完响应后,希望向用户代理(浏览器)发送某种标头,则header()是合适的。

标头函数与返回给客户端的标头相关,我建议您考虑使用cURL执行post请求:

标题函数与返回给客户端的标题相关,我建议您考虑使用cURL执行post请求:

如果您想将用户重定向到另一个页面,您应该使用

header("Location: http://domain.com/some.other.page?x=y");
如果希望用户将POST变量发送到重定向页面,则需要使用JavaScript

<html>
<head>
<title>Redirect</title>
</head>
<body onload="document.myform.submit()">
<form name="myform" action="http://domain.com/some.other.page" method="POST">
<input type="hidden" name="x" value="y">
</form>
</body>
</html>

重新使用

如果要将用户重定向到其他页面,应使用

header("Location: http://domain.com/some.other.page?x=y");
如果希望用户将POST变量发送到重定向页面,则需要使用JavaScript

<html>
<head>
<title>Redirect</title>
</head>
<body onload="document.myform.submit()">
<form name="myform" action="http://domain.com/some.other.page" method="POST">
<input type="hidden" name="x" value="y">
</form>
</body>
</html>

重新使用

扩展timdev的答案,下面是您将使用的旋度:

<?php
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,"http://www.target.com/script.php");
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, "postvar1=value1&postvar2=value2&postvar3=value3");
curl_exec ($ch);
curl_close ($ch); 
?>

扩展timdev的答案,下面是您将使用的旋度:

<?php
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,"http://www.target.com/script.php");
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, "postvar1=value1&postvar2=value2&postvar3=value3");
curl_exec ($ch);
curl_close ($ch); 
?>

我尝试过简单的html表单发布,它对我很有用

    <?php
    if( $dev === 'sample1' || $dev === 'sample2' )  {

?>
    <form name="frmpostdata" action="mystatement.php" method="post">
        <input type="hidden" name="cmsReq" value="MobApp" />
        <input type="hidden" name="cardno" value="<?php echo $chkTkn['cardno'];?>" />
        <input type="hidden" name="cardPwd" value="<?php echo $chkTkn['pwd'];?>" />
        <input type="submit" style="display:none;" name="Submit" value="" />
    </form>
    <script>
        document.frmpostdata.submit();
    </script>
<?php   
        exit;
    }
    ?>


我曾经尝试过简单的html表单发布,它对我很有效

    <?php
    if( $dev === 'sample1' || $dev === 'sample2' )  {

?>
    <form name="frmpostdata" action="mystatement.php" method="post">
        <input type="hidden" name="cmsReq" value="MobApp" />
        <input type="hidden" name="cardno" value="<?php echo $chkTkn['cardno'];?>" />
        <input type="hidden" name="cardPwd" value="<?php echo $chkTkn['pwd'];?>" />
        <input type="submit" style="display:none;" name="Submit" value="" />
    </form>
    <script>
        document.frmpostdata.submit();
    </script>
<?php   
        exit;
    }
    ?>


当你能学到新东西的时候,感觉真好。非常感谢。当你能学到一些新东西的时候真是太好了。非常感谢。