如何从PHP中的HTTP Accept标头中选择内容类型 [英] How to select content type from HTTP Accept header in PHP
问题描述
我正在尝试构建一个符合标准的网站框架,根据浏览器支持,将XHTML 1.1作为application / xhtml + xml或HTML 4.01作为text / html提供。目前它只是在accept头中的任何地方查找application / xhtml + xml,如果它存在则使用它,但这不灵活 - text / html可能有更高的分数。此外,当添加其他格式(WAP,SVG,XForms等)时,它将变得更加复杂。那么,有没有人知道从服务器给出的字符串数组中选择一个经过试验和验证的PHP代码片段,或者是客户端最好支持的字符串,还是基于客户端分数的有序列表?
您可以利用 apache的mod_negotiation模块。这样您就可以使用模块提供的全方位协商功能,包括您自己的首选项的内容类型(例如,我真的想要提供application / xhtml + xml,除非客户端非常喜欢别的东西)。
基本解决方案:
- 使用
AddHandler type-map .var $创建一个.htaccess文件p $ p> as contents
- 使用
创建文件foo.var URI:foo
URI:foo.php / html
内容类型:text / html; qs = 0.7
URI:foo.php / xhtml
内容类型:application / xhtml + xml; qs = 0.8as contents
- 使用
<?php
echo'选择类型创建文件foo.php:' ,substr($ _ SERVER ['PATH_INFO'],1);as contents。
- request http://localhost/whatever/foo.var
为此您需要启用mod_negotiation的工作,AddHandler的相应AllowOverride权限和 AcceptPathInfo 没有为$ _SERVER ['PATH_INFO']禁用。
我的Firefox发送Accept:text / html,application / xhtml + xml,application / xml; q = 0.9, / ;q=0.8和示例.var映射结果是选择类型:xhtml。
您可以使用其他调整来摆脱PATH_INFO或需要请求foo .var ,但基本概念是:让mod_negotiation将请求重定向到您的php脚本,方式是脚本可以读取所选的 内容类型。
那么,有没有人知道一个经过试验和验证的PHP代码选择
这不是纯粹的PHP解决方案但是我会说mod_negotiation已经过试用和测试; - )
I'm trying to build a standard compliant website framework which serves XHTML 1.1 as application/xhtml+xml or HTML 4.01 as text/html depending on the browser support. Currently it just looks for "application/xhtml+xml" anywhere in the accept header, and uses that if it exists, but that's not flexible - text/html might have a higher score. Also, it will become more complex when other formats (WAP, SVG, XForms etc.) are added. So, does anyone know of a tried and tested piece of PHP code to select, from a string array given by the server, either the one best supported by the client or an ordered list based on the client score?
You can leverage apache's mod_negotiation module. This way you can use the full range of negotiation capabilities the module offers, including your own preferences for the content type (e,g, "I really want to deliver application/xhtml+xml, unless the client very much prefers something else"). basic solution:
- create a .htaccess file with
AddHandler type-map .var
as contents
- create a file foo.var with
URI: foo
URI: foo.php/html Content-type: text/html; qs=0.7
URI: foo.php/xhtml Content-type: application/xhtml+xml; qs=0.8as contents
- create a file foo.php with
<?php echo 'selected type: ', substr($_SERVER['PATH_INFO'], 1);
as contents.
- request http://localhost/whatever/foo.var
For this to work you need mod_negotiation enabled, the appropriate AllowOverride privileges for AddHandler and AcceptPathInfo not being disabled for $_SERVER['PATH_INFO'].
With my Firefox sending "Accept: text/html,application/xhtml+xml,application/xml;q=0.9,/;q=0.8" and the example .var map the result is "selected type: xhtml".
You can use other "tweaks" to get rid of PATH_INFO or the need to request foo.var, but the basic concept is: let mod_negotiation redirect the request to your php script in a way that the script can "read" the selected content-type.
So, does anyone know of a tried and tested piece of PHP code to select
It's not a pure php solution but I'd say mod_negotiation has been tried and tested ;-)
这篇关于如何从PHP中的HTTP Accept标头中选择内容类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!