Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get "Content-Type" header of request in PHP

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.

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.

So my question now: How can I access the header "Content-Type" of a request?

like image 645
CodeZombie Avatar asked Apr 01 '11 23:04

CodeZombie


People also ask

How can I get Content-Type header in PHP?

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. Accessing the "Accept" header is a simple as using $_SERVER['HTTP_ACCEPT'] .

What is Content-Type for GET request?

The content type of the requests for these is always x-www-form-urlencoded, since that is all that is allowed for GET and DELETE requests by the HTTP protocol. The client indicates what content type it can accept in the response by supplying an Accept header.

What is Content-Type in request header?

The Content-Type representation header is used to indicate the original media type of the resource (prior to any content encoding applied for sending). In responses, a Content-Type header provides the client with the actual content type of the returned content.

DOES GET request have header?

Yes, you can send any HTTP headers with your GET request. For example, you can send user authentication data in the Authorization header, send browser cookies in the Cookie header, or even send some additional details about your request in custom headers like X-Powered-By or X-User-IP.


2 Answers

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.

This is mandated by the CGI/1.1 specification: http://www.ietf.org/rfc/rfc3875.

like image 187
mario Avatar answered Sep 19 '22 13:09

mario


You'll need to manually instruct Apache to supply the Content-Type header (plus any other headers you want).

Pop something like this in your .htaccess file or virtual host:

RewriteEngine on RewriteRule .* - [E=HTTP_CONTENT_TYPE:%{HTTP:Content-Type},L] 

And voila, you just synthesised your very own $_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.

like image 36
Neil E. Pearson Avatar answered Sep 21 '22 13:09

Neil E. Pearson