Get "Content-Type" Header of Request in PHP

Get Content-Type header of request in PHP

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:

Check Content-type POST request PHP

echo '<pre>';


$allHeaders = getallheaders();
$contentType = $allHeaders['Content-Type'];

Get current Content-Type header in PHP

What does your script look like? When is it executed?
If it is executed at the end of every script, you can check the currently set headers using php function headers-list

Here is an easy example of how to use it:

// headers sent
$headers = headers_list(); // get list of headers
foreach ($headers as $header) { // iterate over that list of headers
if(stripos($header,'Content-Type') !== FALSE) { // if the current header hasthe String "Content-Type" in it
$headerParts = explode(':',$header); // split the string, getting an array
$headerValue = trim($headerParts[1]); // take second part as value

Get PHP content-type from header()

You can use the function headers_list to get the response headers. From there is should be just parsing out the relevant header and rewriting if needed.

  $headers = headers_list();
// get the content type header
foreach($headers as $header){
if (substr(strtolower($header),0,13) == "content-type:"){
list($contentType, $charset) = explode(";", trim(substr($header, 14), 2));
if (strtolower(trim($charset)) != "charset=utf-8"){
header("Content-Type: ".trim($contentType)."; charset=utf-8");

Content-Type header value

You are right in your understanding that the data format does not change based on the value of the "Content-Type" header. It is only an indication to the server, that the client is sending a particular type of content. It is up-to the server to decide how to process a particular type of data.

PHP get the content type header of DOMDocument loaded from url

This is one of those areas where PHP does some automagic behavior that's difficult to discover without many years of experience digging it out. Calling DOMDocument::load() on a URL invokes PHP's http/https stream wrappers to load the URL. Doing so populates a special variable called $http_response_header representing an array of headers from whatever the immediately preceding http/https stream call was.

So right after $xmlDoc->load($url), attempt to inspect $http_response_header. Note that it is not an easily parsed associative array. Instead, you need to find the Content-Type: string and split it on the colon :.

$xmlDoc = new DOMDocument();

// Loop over the array and look for the desired header
foreach ($http_response_header as $header) {
// Find the header with a case-insensitive search
// for Content-Type:
if (stripos($header, 'Content-Type:') === 0) {
// and split it on : to take the second value
// Example: "Content-Type: application/xml; charset=UTF-8"
$content_type = trim(explode(':', $header)[1]);
// You can break out of the loop after finding it

A point of caution - if you are accepting a URL from a form $_POST, you may wish to place some restrictions on what values are acceptable. You could be exposing yourself to some security issues by retrieving any arbitrary URL (denial of service attacks come to mind, possibly proxy abuse too)

// Careful not to accept just any url anyone sends...
$url = $_POST['url'];

Related Topics

Leave a reply