简体   繁体   English

何时在PHP中使用标题('Content-Type:application / json')

[英]When to use header('Content-Type: application/json') in PHP

I've been trying to figure out what's really the usage of header('Content-Type: application/json') in php scripts and I've found different questions and answers on stackoverflow about this subject but I still don't completely get it... 我一直试图弄清楚php脚本中的header('Content-Type: application/json')的用法是什么,我在stackoverflow上找到了关于这个主题的不同问题和答案,但我仍然没有完全得到它...

So here's the question : I've seen in some php projects this line of code, and I'm trying to understand 所以这里有一个问题:我在一些php项目中看到过这行代码,我正在努力理解

  • if this is used when another web page is calling this actual script (with ajax for example) so that the calling page can get a json from the php page 如果在另一个网页调用这个实际脚本(例如ajax)时使用它,那么调用页面可以从php页面获取一个json

OR 要么

  • if this script means that the php page is going to deal with json sent from another web page. 如果这个脚本意味着php页面将处理从另一个网页发送的json。 Or maybe something else ??? 或者别的什么???

Another thing that could help me if answered, lately I've been retrieving json from a resource (external url) with cURL and I had to put this header (Content-type:application/json) in the request. 另一件事可以帮助我,如果回答,最近我一直在使用cURL从资源(外部URL)检索json,我不得不在请求中放置此标头(Content-type:application / json)。 Did I send this header to the exertnal resource or was this MY header so that I can deal with the returned json ? 我是否将此标题发送到exertnal资源或是这个MY标题,以便我可以处理返回的json?

thanks 谢谢

Ok for those who are interested, I finally figured out that header('Content-Type: application/json') is used when another page is calling the php script, so that the other page can automatically parse the result as json . 对于那些感兴趣的人,我终于想通了当另一个页面调用php脚本时使用header('Content-Type: application/json') ,以便其他页面可以自动将结果解析为json

For instance i have in my test.php : 例如我在我的test.php中:

header('Content-type: application/json; charset=utf-8');
$arr = array ('a'=>1,'b'=>2,'c'=>3,'d'=>4,'e'=>5);
echo json_encode($arr); // {"a":1,"b":2,"c":3,"d":4,"e":5}

and in my main.js 在我的main.js

function test() {
    $.ajax({
        url: 'test.php',
        type: 'GET',
        //dataType: 'html',
        success: function (response) {
            alert(response);
        }
    });
};

When I dont have dataType set to "json" or when I don't have the header in my test.php, the alert gives {"a":1,"b":2,"c":3,"d":4,"e":5} which is a string (tried with typeof(response), and when I have this header, or dataType:"json", I get [object Object] from the alert. So this header function is there to indicate to the calling pages which type of data it gives back, so that you can know how to deal with it. In my script, if I didn't have header('Content-Type: application/json') , I would have to parse the response in the javascript like this : JSON.parse(response) in order to make it a json, but with that header, I already have a json object, and I can parse it to html with jSON.stringify(response) . 当我没有将dataType设置为“json”或者我的test.php中没有标题时,警报给出{"a":1,"b":2,"c":3,"d":4,"e":5}这是一个字符串(尝试使用typeof(响应),当我有这个头,或者dataType:“json”时,我从警报中得到[object Object] 。所以这个头函数是在那里向调用页面指示它返回的数据类型,以便您可以知道如何处理它。在我的脚本中,如果我没有header('Content-Type: application/json') ,我我必须解析javascript中的响应,如下所示: JSON.parse(response)为了使它成为一个json,但是有了这个头,我已经有了一个json对象,我可以用jSON.stringify(response)它解析为html jSON.stringify(response)

You should always set the Content-Type for any HTTP response to describe what you're serving in that response. 您应始终为任何HTTP响应设置Content-Type ,以描述您在该响应中所服务的内容。

Whether it's JSON or something else, and whether it's for an AJAX request or any other kind of request. 无论是JSON还是其他东西,以及它是用于AJAX请求还是其他任何类型的请求。


You should also set the Content-Type for any request to describe your POST payload. 您还应该为描述POST有效负载的任何请求设置Content-Type

In PHP, if you don't specify the Content-Type header in the script, it will default to whatever you've configured default-mimetype to be in your php.ini file which is usually text/html . 在PHP中,如果您没有在脚本中指定Content-Type标头,则默认为您配置的default-mimetypephp.ini文件中,通常为text/html

Calling header('Content-Type: application/json') will override that default setting so that the script will respond with that Content-Type when requested. 调用header('Content-Type: application/json')将覆盖该默认设置,以便脚本在请求时响应该Content-Type

Also, when calling curl with a Content-type:application/json header, you're specifying the content type for your request body and not for the expected reponse. 此外,当使用Content-type:application/json标头调用curl时,您要指定请求正文的内容类型,而不是预期的响应。

W3 Description For the Content-Type W3描述内容类型

The purpose of the Content-Type field is to describe the data contained in the body fully enough that the receiving user agent can pick an appropriate agent or mechanism to present the data to the user, or otherwise deal with the data in an appropriate manner. Content-Type字段的目的是充分描述主体中包含的数据,使得接收用户代理可以选择适当的代理或机制来向用户呈现数据,或者以适当的方式处理数据。

Shortly speaking, just to inform the receiver what kind of data he received and consume it accordingly. 简而言之,只是告知接收者他收到了什么样的数据并相应地消费它。

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

相关问题 PHP header('Content-Type: application/json'); 不管用 - PHP header('Content-Type: application/json'); is not working `header(“Content-type:application / json”)的用法;` - The usage of `header(“Content-type:application/json”);` 应该什么时候Content-Type是application / json - When should Content-Type be application/json 如果标题中包含“ Content-Type:application / json”,则不会创建PHP 5.4.9 $ _POST超全局变量 - PHP 5.4.9 $_POST superglobal not created if 'Content-Type: application/json' is in header PHP中的内容类型标题 - Content-Type Header in PHP Slim - 如何使用“Content-Type:application / json”标题发送响应? - Slim - How to send response with “Content-Type: application/json” header? 标头Content-Type:application / json返回text / html - header Content-Type: application/json returning text/html 为什么是header('Content-Type:application / json'); 在我们的网络服务中? - Why header('Content-Type: application/json'); in our web service? 未记录的PHP自动将content-type:application / json解码为$ _REQUEST - PHP undocumented automatically decode content-type:application/json to $_REQUEST Nginx,fastcgi PHP 请求体在内容类型为 application/json 时为空 - Nginx, fastcgi PHP request body empty when content-type application/json
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM