Skip to content

Commit c7f4d74

Browse files
committed
getHeadder(...)
1 parent 6699f4e commit c7f4d74

File tree

1 file changed

+21
-1
lines changed

1 file changed

+21
-1
lines changed

src/Util.php

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -570,6 +570,23 @@ public static function setStatus($status) {
570570
http_response_code($status);
571571
}
572572

573+
public static function getHeader($header, $headers = null) {
574+
$headers = $headers ? $headers : getallheaders();
575+
foreach ($headers as $key => $value) {
576+
577+
// if $headers is a non-associative array e.g. headers_list()
578+
if (is_int($key)) {
579+
$parts = explode(':', $value);
580+
$key = $parts[0];
581+
$value = trim($parts[1]);
582+
}
583+
584+
if (strtolower($key) === strtolower($header)) return $value;
585+
}
586+
587+
return false;
588+
}
589+
573590
public static function setContentType($type = 'application/json') {
574591
header('Content-Type: ' . $type);
575592
}
@@ -579,7 +596,10 @@ public static function debug($var, $options = null, $return = false) {
579596
$is_ajax = self::isAjax();
580597
$is_pjax = self::isPjax();
581598

582-
$is_html = !($is_cli || $is_ajax) || $is_pjax;
599+
// if current header is json, return plain text
600+
$content_type = self::getHeader('Content-Type', headers_list());
601+
602+
$is_html = !($is_cli || $is_ajax || $content_type === 'application/json') || $is_pjax;
583603
$new_line = self::get('newline', $options, true);
584604

585605
$info = print_r($var, true);

0 commit comments

Comments
 (0)