php-防止对其他函数的调用更改标题
我有以下代码:php-防止对其他函数的调用更改标题,php,header,echo,content-type,Php,Header,Echo,Content Type,我有以下代码: $result = array('output'=>'success'); header("Content-Type: application/json; Charset=UTF-8"); echo json_encode($result); 但是,当我按如下方式调用另一个函数时,标题将更改为text/html $myfunc->call(); $result = array('output'=>'success'); header("Content-Type
$result = array('output'=>'success');
header("Content-Type: application/json; Charset=UTF-8");
echo json_encode($result);
但是,当我按如下方式调用另一个函数时,标题将更改为text/html
$myfunc->call();
$result = array('output'=>'success');
header("Content-Type: application/json; Charset=UTF-8");
echo json_encode($result);
基本上
$myfunc->call()代码>更改标题并回显文本。这导致我的代码失败。如何忽略$myfunc->call()中标题和输出的更改代码>?我能想到的唯一一件事是向该函数传递一个参数,指示是否应执行头代码,然后将该段代码包装在一个条件语句中。但是,只有在您可以自由更改函数的情况下,才可以这样做。否则,您将被卡住,因为无法设置两次标题
function call($setHeaders=true) {
...
if ($setHeaders) {
// Code for seting the header
}
}
“忽略”函数/方法的输出的唯一方法是不要调用它,或者使用return
关键字,而不是echo
或print
>因为无法设置两次标题,所以被卡住了。好啊