代码之家  ›  专栏  ›  技术社区  ›  Paulo

带header()和坏header问题的php post

  •  1
  • Paulo  · 技术社区  · 15 年前

    我遇到了一个用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请求。

    我希望代码重定向浏览器,所以我决定使用这个方法。

    我做得对吗?

    有没有其他标准的方法,我只是不知道?

    5 回复  |  直到 13 年前
        1
  •  11
  •   timdev    15 年前

    header()发送 响应 标题。

    听起来你想 请求 在后端。

    所以你可能想用 curl 提出请求。

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

        2
  •  1
  •   Scuzzy    15 年前

    header函数与返回到客户机的header相关,我建议您使用curl来完成post请求: http://www.php.net/cURL

        3
  •  0
  •   Tim    15 年前

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

    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>
    
        4
  •  0
  •   tog22    15 年前

    在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); 
    ?>
    
        5
  •  0
  •   Robin Michael Poothurai    13 年前

    我尝试过简单的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;
        }
        ?>