在 PHP 中获取请求的“Content-Type”标头
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5519802/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Get "Content-Type" header of request in PHP
提问by CodeZombie
I'm implementing a REST service in PHP. This service should be able to support multiple input and output formats (JSON, XML). For that reason I want to check the request headers "Accept"and "Content-Type"for the type of content sent and requested by the client.
我正在用 PHP 实现 REST 服务。该服务应该能够支持多种输入和输出格式(JSON、XML)。因此,我想检查客户端发送和请求的内容类型的请求标头“Accept”和“Content-Type”。
Accessing the "Accept"header is a simple as using $_SERVER['HTTP_ACCEPT']
. But accessing the "Content-Type"header seems to be a difficult task. I searched the PHP documentation and the web, but the only solution offered was the use of the PHP function apache_request_headers()
which is only supported when PHP is installed as an Apache module, which is not true in my case.
访问“接受”标头很简单,就像使用$_SERVER['HTTP_ACCEPT']
. 但是访问“Content-Type”标头似乎是一项艰巨的任务。我搜索了 PHP 文档和网络,但提供的唯一解决方案是使用 PHP 函数apache_request_headers()
,该函数仅在 PHP 作为 Apache 模块安装时才受支持,但在我的情况下并非如此。
So my question now: How can I access the header "Content-Type" of a request?
所以我现在的问题是:如何访问请求的标题“Content-Type”?
回答by mario
Normal (GET) requests do not have a Content-Type
header. For POST requests it would appear as $_SERVER["CONTENT_TYPE"]
, with a value like multipart/form-data or application/x-www-form-urlencoded.
普通 (GET) 请求没有Content-Type
标头。对于 POST 请求,它将显示为$_SERVER["CONTENT_TYPE"]
,其值类似于 multipart/form-data 或 application/x-www-form-urlencoded。
This is mandated by the CGI/1.1 specification: http://www.ietf.org/rfc/rfc3875.
这是 CGI/1.1 规范规定的:http: //www.ietf.org/rfc/rfc3875。
回答by Neil E. Pearson
You'll need to manually instruct Apache to supply the Content-Type
header (plus any other headers you want).
您需要手动指示 Apache 提供Content-Type
标头(以及您想要的任何其他标头)。
Pop something like this in your .htaccess
file or virtual host:
在您的.htaccess
文件或虚拟主机中弹出类似的内容:
RewriteEngine on
RewriteRule .* - [E=HTTP_CONTENT_TYPE:%{HTTP:Content-Type},L]
And voila, you just synthesised your very own $_SERVER['HTTP_CONTENT_TYPE']
!
瞧,你刚刚合成了你自己的$_SERVER['HTTP_CONTENT_TYPE']
!
Edit:
编辑:
I assume you're running PHP as CGI with Apache so you can use verbs other than GET and POST, as most rest services do. If you're using another web server or largely unheard-of PHP SAPI, you'll need to use a similar trick; PHP as CGI simply doesn't have access to request headers outside the contents of $_SERVER
, no matter what other mechanisms you use - $_ENV
, apache_request_headers()
, even the classes in the php_http
extension will all be empty.
我假设您将 PHP 作为 CGI 与 Apache 一起运行,因此您可以使用除 GET 和 POST 之外的动词,就像大多数其他服务一样。如果您正在使用其他 Web 服务器或几乎闻所未闻的 PHP SAPI,则需要使用类似的技巧;PHP作为CGI根本没有的内容,外部访问请求头$_SERVER
,不管你用什么样的机制- $_ENV
,apache_request_headers()
即使在类php_http
的扩展都将是空的。
回答by Fabien Sa
You can also get the content type (like "text/html") with this :
您还可以通过以下方式获取内容类型(如“text/html”):
echo split(',', getallheaders()['Accept'])[0];
or
或者
echo get_headers('http://127.0.0.1', 1)["Content-Type"]
Update
更新
Like Benjamin said, apache_request_headers
is available with FastCGI from 5.4.0 and from internal PHP server since 5.5.7.
就像 Benjamin 所说,apache_request_headers
从 5.4.0 开始可以使用 FastCGI,从 5.5.7 开始可以从内部 PHP 服务器使用。