[英]Get “Content-Type” header of request in PHP
I'm implementing a REST service in PHP. 我正在用PHP实现REST服务。 This service should be able to support multiple input and output formats (JSON, XML). 该服务应该能够支持多种输入和输出格式(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. 出于这个原因,我想检查请求标题“Accept”和“Content-Type” ,了解客户端发送和请求的内容类型。
Accessing the "Accept" header is a simple as using $_SERVER['HTTP_ACCEPT']
. 访问“Accept”标头很简单,就像使用$_SERVER['HTTP_ACCEPT']
。 But accessing the "Content-Type" header seems to be a difficult task. 但访问“Content-Type”标题似乎是一项艰巨的任务。 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. 我搜索了PHP文档和Web,但提供的唯一解决方案是使用PHP函数apache_request_headers()
,仅当PHP作为Apache模块安装时才支持,在我的情况下不是这样。
So my question now: How can I access the header "Content-Type" of a request? 所以现在我的问题是:如何访问请求的标题“Content-Type”?
Normal (GET) requests do not have a Content-Type
header. 普通(GET)请求没有Content-Type
标头。 For POST requests it would appear as $_SERVER["CONTENT_TYPE"]
, with a value like multipart/form-data or application/x-www-form-urlencoded. 对于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 。
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. 我假设您使用Apache运行PHP作为CGI,因此您可以使用除GET和POST之外的动词,就像大多数其他服务一样。 If you're using another web server or largely unheard-of PHP SAPI, you'll need to use a similar trick; 如果您正在使用其他Web服务器或者基本上闻所未闻的PHP SAPI,您将需要使用类似的技巧; 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根本无法访问$_SERVER
内容之外的请求标头,无论你使用什么其他机制 - $_ENV
, apache_request_headers()
,甚至php_http
扩展中的类都将为空。
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服务器。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.