简体   繁体   中英

php - Prevent call to other function from changing headers

I have the following code:

$result = array('output'=>'success');
header("Content-Type: application/json; Charset=UTF-8");
echo json_encode($result);

However, when I call another function as follows, the header is changed to text/html

$myfunc->call();
$result = array('output'=>'success');
header("Content-Type: application/json; Charset=UTF-8");
echo json_encode($result);

Basically $myfunc->call(); changes the header and echoes text. This is causing my code to fail. How can I ignore the change of header and output from $myfunc->call(); ?

The only thing I can think of is to pass a parameter to that function indicating whether the header code should be executed or not, then wrapping that piece of code in a conditional. However, thats only if you are free to change the function. Otherwise you are stuck because you can't set the headers twice.

function call($setHeaders=true) {
    ...
    if ($setHeaders) {
        // Code for seting the header
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM