我正在尝试构建一个符合标准的网站框架,根据浏览器支持,将XHTML 1.1作为application/xhtml + xml或HTML 4.01作为text/html提供.目前它只是在accept头中的任何地方查找"application/xhtml + xml",如果它存在则使用它,但这不灵活 - text/html可能有更高的分数.此外,当添加其他格式(WAP,SVG,XForms等)时,它将变得更加复杂.那么,有没有人知道从服务器给出的字符串数组中选择一个经过试验和验证的PHP代码,要么是客户最好支持的,要么是基于客户得分的有序列表?
Mac*_*ski 20
我图书馆的小片段:
function getBestSupportedMimeType($mimeTypes = null) {
// Values will be stored in this array
$AcceptTypes = Array ();
// Accept header is case insensitive, and whitespace isn’t important
$accept = strtolower(str_replace(' ', '', $_SERVER['HTTP_ACCEPT']));
// divide it into parts in the place of a ","
$accept = explode(',', $accept);
foreach ($accept as $a) {
// the default quality is 1.
$q = 1;
// check if there is a different quality
if (strpos($a, ';q=')) {
// divide "mime/type;q=X" into two parts: "mime/type" i "X"
list($a, $q) = explode(';q=', $a);
}
// mime-type $a is accepted with the quality $q
// WARNING: $q == 0 means, that mime-type isn’t supported!
$AcceptTypes[$a] = $q;
}
arsort($AcceptTypes);
// if no parameter was passed, just return parsed data
if (!$mimeTypes) return $AcceptTypes;
$mimeTypes = array_map('strtolower', (array)$mimeTypes);
// let’s check our supported types:
foreach ($AcceptTypes as $mime => $q) {
if ($q && in_array($mime, $mimeTypes)) return $mime;
}
// no mime-type found
return null;
}
Run Code Online (Sandbox Code Playgroud)
示例用法:
$mime = getBestSupportedMimeType(Array ('application/xhtml+xml', 'text/html'));
Run Code Online (Sandbox Code Playgroud)
Vol*_*erK 11
您可以利用apache的mod_negotiation模块.通过这种方式,您可以使用模块提供的全部协商功能,包括您自己的内容类型首选项(例如,"我真的想要提供application/xhtml + xml,除非客户端非常喜欢其他内容") .基本解决方案
AddHandler type-map .var作为内容
URI: foo作为内容
URI: foo.php/html Content-type: text/html; qs=0.7
URI: foo.php/xhtml Content-type: application/xhtml+xml; qs=0.8
<?php echo 'selected type: ', substr($_SERVER['PATH_INFO'], 1);作为内容.
为此,您需要启用mod_negotiation,为$ _SERVER ['PATH_INFO']禁用AddHandler和AcceptPathInfo的相应AllowOverride权限.
我的Firefox发送"Accept:text/html,application/xhtml + xml,application/xml; q = 0.9,/ ; q = 0.8",示例.var映射结果为"selected type:xhtml".
您可以使用其他"调整"来摆脱PATH_INFO或需要请求foo .var,但基本概念是:让mod_negotiation将请求重定向到您的php脚本,方式是脚本可以"读取"所选内容 -类型.
那么,有没有人知道一个经过试验和测试的PHP代码可供选择这不是一个纯粹的PHP解决方案,但我会说mod_negotiation已经过试用和测试;-)
Vol*_*erK 10
Pear :: HTTP 1.4.1有一个方法字符串negotiateMimeType(数组$支持,字符串$ default)
<?php
require 'HTTP.php';
foreach(
array(
'text/*;q=0.3, text/html;q=0.7, text/html;level=1, text/html;level=2;q=0.4, */*;q=0.5',
'text/*;q=0.3, text/html;q=0.8, application/xhtml+xml;q=0.7, */*;q=0.2',
'text/*;q=0.3, text/html;q=0.7, */*;q=0.8',
'text/*, application/xhtml+xml',
'text/html, application/xhtml+xml'
) as $testheader) {
$_SERVER['HTTP_ACCEPT'] = $testheader;
$http = new HTTP;
echo $testheader, ' -> ',
$http->negotiateMimeType( array('application/xhtml+xml', 'text/html'), 'application/xhtml+xml'),
"\n";
}
Run Code Online (Sandbox Code Playgroud)
版画
text/*;q=0.3, text/html;q=0.7, text/html;level=1, text/html;level=2;q=0.4, /;q=0.5 -> application/xhtml+xml text/*;q=0.3, text/html;q=0.8, application/xhtml+xml;q=0.7, */*;q=0.2 -> text/html text/*;q=0.3, text/html;q=0.7, */*;q=0.8 -> application/xhtml+xml text/*, application/xhtml+xml -> application/xhtml+xml text/html, application/xhtml+xml -> text/html