如何在PHP中处理HTTP Expect标头

时间:2016-09-04 19:01:31

标签: php curl http-headers

我正在使用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;
}

1 个答案:

答案 0 :(得分:2)

在服务器端,这应该由您的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似乎会继续,好像收到了响应一样#34;如果您没有足够新的版本,Expect是HTTP 1.1标头,那么将请求作为HTTP 1.0发送应该可以解决问题。