简体   繁体   English

PHP中的内容类型标题

[英]Content-Type Header in PHP

I am creating a wordpress plugin which fetched some data and then echos a json_encode . 我正在创建一个wordpress插件,该插件会提取一些数据,然后回显json_encode

This is being done by creating a function and calling it from a shortcode. 这是通过创建一个函数并从短代码中调用它来完成的。

The shortcode is being injected in blank page template from wordpress pages. 简码被从wordpress页面注入空白页面模板中。

I am trying to set the header to application/json in the function of the shortcode plugin however this is being ignore and the content type is still set to HTML/TEXT. 我正在尝试在shortcode插件的功能中将标头设置为application / json,但是这被忽略,内容类型仍设置为HTML / TEXT。

I am setting the header this way: 我这样设置标题:

header('Content-Type: application/json'); 

The whole plugin thing looks like this: 整个插件看起来像这样:

<?php function abc_func(){

    header('content-type: application/json; charset=utf-8');

    $abc_array = array(
                    'abc' => $abc
    );

    echo json_encode($abc_array);

}

add_shortcode('abc_shortcode', 'abc_func');

?>

这可能导致在设置标题(或在您的插件代码之前)之前,有些数据已发送到输出,请确保您在这些标题之前设置了标题。

Check your php.ini file for the output_buffering setting. 检查您的php.ini文件中的output_buffering设置。 If it's not set to "off" than PHP is automatically doing output buffering for you. 如果未将其设置为“ off”,则PHP会自动为您进行输出缓冲。 Set that to off and echo something before the header command, and you should see the "classic error". 将其设置为off并在标题命令之前回显某些内容,您应该会看到“经典错误”。

Content-type not working in PHP 内容类型在PHP中不起作用

Try this. 尝试这个。

header('content-type: application/json; charset=utf-8');

Is there any place that changed the content type later? 以后有什么地方可以更改内容类型吗?

Edited 2017-03-02 编辑2017-03-02

Content-type not working in PHP 内容类型在PHP中不起作用

Check your php.ini file for the output_buffering setting. 检查您的php.ini文件中的output_buffering设置。 If it's not set to "off" than PHP is automatically doing output buffering for you. 如果未将其设置为“ off”,则PHP会自动为您进行输出缓冲。 Set that to off and echo something before the header command, and you should see the "classic error". 将其设置为off并在标题命令之前回显某些内容,您应该会看到“经典错误”。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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