【问题标题】:How do you POST to a page using the PHP header() function?如何使用 PHP header() 函数发布到页面?
【发布时间】:2010-10-13 18:39:27
【问题描述】:

我在here 上发现了以下代码,我认为它可以满足我的需求,但它不起作用:

$host = "www.example.com";
$path = "/path/to/script.php";
$data = "data1=value1&data2=value2";
$data = urlencode($data);

header("POST $path HTTP/1.1\r\n");
header("Host: $host\r\n");
header("Content-type: application/x-www-form-urlencoded\r\n");
header("Content-length: " . strlen($data) . "\r\n");
header("Connection: close\r\n\r\n");
header($data);

我希望在不将用户发送到中间页面然后使用 JavaScript 重定向它们的情况下发布表单数据。我也不想使用 GET,所以使用后退按钮并不容易。

这段代码有问题吗?还是有更好的方法?

编辑 我在想标题函数会做什么。我在想我可以让浏览器将数据发回服务器,但这不是它的本意。相反,我在我的代码中找到了一种方法,可以完全避免发布帖子(不会中断,只是继续切换到下一个案例)。

【问题讨论】:

标签: post header http-headers


【解决方案1】:

header 函数用于将 HTTP 响应头发送回用户(即您不能使用它来创建请求头。

请问您为什么要这样做?为什么要模拟 POST 请求,因为您可以就在那儿,然后以某种方式对数据采取行动?我当然假设 script.php 驻留在您的服务器上。

要创建 POST 请求,请使用 fsockopen() 打开与主机的 TCP 连接,然后在 fsockopen() 返回的处理程序上使用 fwrite(),其值与您在 OP 中的标头函数中使用的值相同。或者,您可以使用 cURL。

【讨论】:

  • 谢谢,我有点困惑,是的,你是对的,脚本中有更好的方法,不需要发布。
【解决方案2】:

除了 Salaryman 所说的,看看 PEAR 中的类,那里有 HTTP 请求类,即使你的 PHP 发行版中没有安装 cURL 扩展,你也可以使用它们。

【讨论】:

    【解决方案3】:
    private function sendHttpRequest($host, $path, $query, $port=80){
        header("POST $path HTTP/1.1\r\n" );
        header("Host: $host\r\n" );
        header("Content-type: application/x-www-form-urlencoded\r\n" );
        header("Content-length: " . strlen($query) . "\r\n" );
        header("Connection: close\r\n\r\n" );
        header($query);
    }
    

    这会让你马上得到帮助

    【讨论】:

    • 这对我不起作用 - 我一添加该代码就会收到一个(无信息的)内部服务器错误。
    • 为什么不使用时参数签名中有$port
    • @Zurechtweiser 他可能问了,所以你可以回答
    【解决方案4】:

    有一个很好的课程可以做你想做的事。下载地址:http://sourceforge.net/projects/snoopy/

    【讨论】:

      【解决方案5】:

      今天非常需要这个问题的答案,因为不是每个人都想使用 cURL 来使用 Web 服务。 PHP 也允许使用以下代码进行此操作

      function get_info()
      {
          $post_data = array(
              'test' => 'foobar',
              'okay' => 'yes',
              'number' => 2
          );
      
          // Send a request to example.com
          $result = $this->post_request('http://www.example.com/', $post_data);
      
          if ($result['status'] == 'ok'){
      
              // Print headers
              echo $result['header'];
      
              echo '<hr />';
      
              // print the result of the whole request:
              echo $result['content'];
      
          }
          else {
              echo 'A error occured: ' . $result['error'];
          }
      
      }
      
      function post_request($url, $data, $referer='') {
      
          // Convert the data array into URL Parameters like a=b&foo=bar etc.
          $data = http_build_query($data);
      
          // parse the given URL
          $url = parse_url($url);
      
          if ($url['scheme'] != 'http') {
              die('Error: Only HTTP request are supported !');
          }
      
          // extract host and path:
          $host = $url['host'];
          $path = $url['path'];
      
          // open a socket connection on port 80 - timeout: 30 sec
          $fp = fsockopen($host, 80, $errno, $errstr, 30);
      
          if ($fp){
      
              // send the request headers:
              fputs($fp, "POST $path HTTP/1.1\r\n");
              fputs($fp, "Host: $host\r\n");
      
              if ($referer != '')
                  fputs($fp, "Referer: $referer\r\n");
      
              fputs($fp, "Content-type: application/x-www-form-urlencoded\r\n");
              fputs($fp, "Content-length: ". strlen($data) ."\r\n");
              fputs($fp, "Connection: close\r\n\r\n");
              fputs($fp, $data);
      
              $result = '';
              while(!feof($fp)) {
                  // receive the results of the request
                  $result .= fgets($fp, 128);
              }
          }
          else {
              return array(
                  'status' => 'err',
                  'error' => "$errstr ($errno)"
              );
          }
      
          // close the socket connection:
          fclose($fp);
      
          // split the result header from the content
          $result = explode("\r\n\r\n", $result, 2);
      
          $header = isset($result[0]) ? $result[0] : '';
          $content = isset($result[1]) ? $result[1] : '';
      
          // return as structured array:
          return array(
              'status' => 'ok',
              'header' => $header,
              'content' => $content);
      
      }
      

      【讨论】:

        猜你喜欢
        • 2012-03-02
        • 2012-04-08
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-08-09
        相关资源
        最近更新 更多