php 设置 HTTP 请求“内容类型”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9823810/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 07:42:57  来源:igfitidea点击:

Set HTTP Request "Content-Type"

phpcurlhttp-headers

提问by Michiel

How do you set the content type of an HTTP Request?

如何设置 HTTP 请求的内容类型?

I've tried this:

我试过这个:

$headers['Accept'] = 'application/xml';
$headers['Content-Type'] = 'application/xml';

curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);

And this is the result:

这是结果:

HTTP/1.1 200 OK
Content-Encoding: gzip
Content-Type: text/html;charset=UTF-8
Date: Thu, 22 Mar 2012 14:04:36 GMT

but no luck so far...
What do I have to do to get an Content-Type: application/xmlin my HTTP response?

但到目前为止没有运气......
我必须做什么才能Content-Type: application/xml在我的 HTTP 响应中获得一个?

回答by Michael Berkowski

I believe the headers must be a plain array whose elements are full key:value headers, not an associative array:

我相信标题必须是一个普通数组,其元素是完整的键:值标题,而不是关联数组:

$headers = array();
$headers[] = 'Accept: application/xml';
$headers[] = 'Content-Type: application/xml';

curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);

This is specified in the curl_setopt()documentation.

这是在文档中指定的curl_setopt()

回答by Tony Miller

You set an accept type for your request (and use Michael's answer for that, you should count his answer as answering the direct question, because it does). That doesn't mandate that the server resonds with that content type.

您为您的请求设置了一个接受类型(并为此使用Michael的回答,您应该将他的回答视为回答直接问题,因为确实如此)。这并不强制要求服务器响应该内容类型。

If the other end of the request is a static file, you have to make sure your web server sends that file with a MIME type of application/xml. If it ends with .xml, then both Apache and IIS will already do this. If it's something else that is a non-standard file extension, but you want it to be sent as application/xml, then you'll have to get the server manager to set the httpd.conf or .htaccess to add the mime type for the file. In IIS you use the GUI admin tools to do the same thing, adding a mime type for the file extension as application/xml.

如果请求的另一端是静态文件,则您必须确保您的 Web 服务器使用 MIME 类型的 application/xml 发送该文件。如果它以 .xml 结尾,那么 Apache 和 IIS 都已经这样做了。如果它是其他非标准文件扩展名,但您希望将其作为 application/xml 发送,那么您必须让服务器管理器设置 httpd.conf 或 .htaccess 以添加 mime 类型文件。在 IIS 中,您使用 GUI 管理工具来做同样的事情,为文件扩展名添加一个 mime 类型作为 application/xml。

If the other end of the request is a server side scripting language such as PHP, Perl, Python, ColdFusion, ASP, ASP.net, etc etc then you need to use the appropriate method/function in that language for the script being called to emit the content type header and set it to application/xml.

如果请求的另一端是服务器端脚本语言,例如 PHP、Perl、Python、ColdFusion、ASP、ASP.net 等,那么您需要使用该语言中的适当方法/函数来调用被调用的脚本发出内容类型标头并将其设置为 application/xml。

Update: You say in comments you're using WizTools to emit a request that does get a return of application/xml. If you want to clone that environment, then send ALL the headers it's sending in your curl request. One guess is that user agent might be in play.

更新:您在评论中说您正在使用 WizTools 发出一个确实获得 application/xml 返回的请求。如果要克隆该环境,请发送它在 curl 请求中发送的所有标头。一种猜测是用户代理可能在起作用。