【问题标题】:PHP POST with header() and Bad header problems带有 header() 和 Bad header 问题的 PHP POST
【发布时间】:2010-12-07 20:55:03
【问题描述】:

我遇到了一个在 PHP 中发送标头的令人震惊的问题。我已经在 SO 和其他网站上阅读了大约 45 分钟,但我无法为我的问题找到合理的理由。

我需要向另一台服务器发送 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 发布请求。

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

我做得对吗?

还有其他一些我不知道的标准方法吗?

【问题讨论】:

    标签: php http


    【解决方案1】:

    header() 发送一个 response 标头。

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

    所以你可能想使用curl 来发出请求。

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

    【讨论】:

    • 能学到新东西真是太好了。非常感谢。
    【解决方案2】:

    标头函数与返回给客户端的标头有关,我建议您考虑使用 cURL 来执行您的 post 请求:http://www.php.net/cURL

    【讨论】:

      【解决方案3】:

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

      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】:

        扩展 timdev 的答案,这是您要使用的 cURL:

        <?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】:

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

          【讨论】:

            猜你喜欢
            • 2017-08-10
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2021-11-09
            • 1970-01-01
            • 2011-02-21
            • 2014-10-10
            • 2012-01-14
            相关资源
            最近更新 更多