【问题标题】:How to handle HTTP Expect header in PHP如何在 PHP 中处理 HTTP Expect 标头
【发布时间】:2017-01-12 05:26:45
【问题描述】:

我正在使用 curl 命令通过 POST 请求发送文件。我没有使用 PHP curl 库。但是,curl 命令太客气了,发送了一个 Expect 标头。我应该如何在服务器端处理它?我知道服务器必须返回一个 100-continue 响应,但是它如何继续接收文件呢? $_FILES 数组始终为空。

这是我发送到服务器的:

/usr/bin/curl -sS -X POST -T "/tmp/phpuPfIDd" http://localhost/stats/rgateway/index.php/data 2>&1

而服务器端代码是这样的:

<?php
session_start();

switch($_SERVER['REQUEST_METHOD']) {
    case 'GET':
        # Do something if it is a GET request   
        break;

    case 'POST':    
        $headers = apache_request_headers();
        if (array_key_exists('Expect', $headers)) {
                 # What should I do here in order to receive uploaded the file?
        }   
        break;
}

【问题讨论】:

  • 请澄清您的具体问题或添加其他详细信息以准确突出您的需要。正如目前所写的那样,很难准确地说出你在问什么。请参阅How to Ask 页面以获得澄清此问题的帮助。
  • 谢谢。现在我已经编辑了帖子。
  • -T, --upload-file &lt;file&gt; This transfers the specified local file to the remote URL. If there is no file part in the specified URL, Curl will append the local file name. NOTE that you must use a trailing / on the last directory to really prove to Curl that there is no file name or curl will think that your last directory name is the remote file name to use. That will most likely cause the upload operation to fail
  • 您的标头 Expect 也没有价值,因此无效。 Apache 比热锅更快地丢弃无效标头。我不确定你为什么不使用 php cURL 模块。它更容易使用。如果要上传文件,需要使用正确的Content-Type header。
  • 整个过程由网络服务器(Apache 或 Nginx)w3.org/Protocols/rfc2616/rfc2616-sec8.html#sec8.2.3 处理

标签: php curl http-headers


【解决方案1】:

在服务器端,这应该由你的 web 服务器来处理,PHP 不需要担心这些低级的事情。

来自man curl的相关部分:

   -0, --http1.0
          (HTTP) Tells curl to use HTTP version 1.0 instead of using its
          internally preferred: HTTP 1.1.

   --expect100-timeout <seconds>
          (HTTP)  Maximum  time in seconds that you allow curl to wait for a
          100-continue response when curl emits an Expects: 100-continue
          header in its request. By default curl will wait one second. This 
          option accepts decimal values! When curl stops  waiting, it will
          continue as if the response has been received.

          (Added in 7.47.0)

因此,如果您有足够新的版本,cURL 似乎会“继续,就像收到响应一样”。如果您没有足够新的版本,Expect 是 HTTP 1.1 标头,因此以 HTTP 1.0 发送请求应该可以解决问题。

【讨论】:

  • 实际上这不太正确 - expect 应该由 PHP 脚本处理,以便脚本可以根据服务器不会也不应该知道的任何逻辑或参数来决定是否允许此请求或拒绝的。
  • 不,HTTP 服务器会像这样处理低级协议细节。 PHP 根据初始请求决定是否提供文件。
  • 不,请参考 IETF 讨论,EXPECT 背后的全部意图是让接收节点在客户端可以发送大正文之前做出决定,例如大上传。
  • 这很有趣,我不知道它背后的意图。我很想知道 Web 服务器(Apache 或 Nginx)是否将其传递给 PHP 并允许它响应;我可能不得不添加到这个答案。谢谢!
猜你喜欢
  • 2012-12-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-12-27
  • 2013-06-02
  • 1970-01-01
相关资源
最近更新 更多