protected function _detect_output_format()
{
// Concatenate formats to a regex pattern e.g. \.(csv|json|xml)
$pattern = '/\\.(' . implode('|', array_keys($this->_supported_formats)) . ')($|\\/)/';
$matches = [];
// Check if a file extension is used e.g. http://example.com/api/index.json?param1=param2
if (preg_match($pattern, $this->uri->uri_string(), $matches)) {
return $matches[1];
}
// Get the format parameter named as 'format'
if (isset($this->_get_args['format'])) {
$format = strtolower($this->_get_args['format']);
if (isset($this->_supported_formats[$format]) === TRUE) {
return $format;
}
}
// Get the HTTP_ACCEPT server variable
$http_accept = $this->input->server('HTTP_ACCEPT');
// Otherwise, check the HTTP_ACCEPT server variable
if ($this->config->item('rest_ignore_http_accept') === FALSE && $http_accept !== NULL) {
// Check all formats against the HTTP_ACCEPT header
foreach (array_keys($this->_supported_formats) as $format) {
// Has this format been requested?
if (strpos($http_accept, $format) !== FALSE) {
if ($format !== 'html' && $format !== 'xml') {
// If not HTML or XML assume it's correct
return $format;
} elseif ($format === 'html' && strpos($http_accept, 'xml') === FALSE) {
// HTML or XML have shown up as a match
// If it is truly HTML, it wont want any XML
return $format;
} else {
if ($format === 'xml' && strpos($http_accept, 'html') === FALSE) {
// If it is truly XML, it wont want any HTML
return $format;
}
}
}
}
}
// Check if the controller has a default format
if (empty($this->rest_format) === FALSE) {
return $this->rest_format;
}
// Obtain the default format from the configuration
return $this->_get_default_output_format();
}