如何从PHP中的HTTP Accept标头中选择内容类型

l0b*_*0b0 18 php http-headers

我正在尝试构建一个符合标准的网站框架,根据浏览器支持,将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,除非客户端非常喜欢其他内容") .基本解决方案

  • 用.创建.htaccess文件
    AddHandler type-map .var
    作为内容
  • 用.创建一个文件foo.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
    作为内容
  • 用.创建一个文件foo.php
    <?php
    echo 'selected type: ', substr($_SERVER['PATH_INFO'], 1);
    作为内容.
  • 请求http://localhost/whatever/foo.var

为此,您需要启用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已经过试用和测试;-)

  • 我更喜欢将这个逻辑保留在我的代码中. (5认同)

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

编辑:毕竟这可能不是那么好...
我的firefox发送Accept:text/html,application/xhtml + xml,application/xml; q = 0.9,/ ; q = 0.8
text/html和application/xhtml + xml有q = 1.0但是PEAR :: HTTP(afaik)不允许选择你喜欢哪一个,它会返回text/html,无论你传递的是$支持.这对您来说可能是也可能不够.看到我的其他答案.


Wil*_*and 9

只是为了记录,Negotiation是一个用于处理内容协商的纯PHP实现.