获取“内容类型” PHP中的请求标头 [英] Get "Content-Type" header of request in PHP
问题描述
我正在用PHP实现REST服务。该服务应该能够支持多种输入和输出格式(JSON,XML)。出于这个原因,我想检查请求标题接受和内容类型,了解客户发送和请求的内容类型。
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.
访问Accept标题非常简单,只需使用 $ _ SERVER ['HTTP_ACCEPT']
即可。但访问Content-Type标题似乎是一项艰巨的任务。我搜索了PHP文档和Web,但提供的唯一解决方案是使用PHP函数 apache_request_headers()
,仅当PHP作为Apache模块安装时才支持,在我的情况下这不是真的。
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.
所以现在我的问题是:我如何访问请求的标题Content-Type?
So my question now: How can I access the header "Content-Type" of a request?
推荐答案
普通(GET)请求没有 Content-Type
标头。对于POST请求,它将显示为 $ _ SERVER [CONTENT_TYPE]
,其值类似于multipart / form-data或application / x-www -form-urlencoded。
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.
这是CGI / 1.1规范规定的: http://www.ietf.org/rfc/rfc3875 。
This is mandated by the CGI/1.1 specification: http://www.ietf.org/rfc/rfc3875.
这篇关于获取“内容类型” PHP中的请求标头的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!