How to select content type from HTTP Accept header in PHP

后端 未结 7 1048
[愿得一人]
[愿得一人] 2020-12-16 13:10

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. Curre

7条回答
  •  太阳男子
    2020-12-16 13:21

    Pear::HTTP 1.4.1 has a method string negotiateMimeType( array $supported, string $default)

     ',
        $http->negotiateMimeType( array('application/xhtml+xml', 'text/html'), 'application/xhtml+xml'),
        "\n";
    }
    

    prints

    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

    edit: this might not be so good after all...
    My firefox sends Accept: text/html,application/xhtml+xml,application/xml;q=0.9,/;q=0.8
    text/html and application/xhtml+xml have q=1.0 but PEAR::HTTP (afaik) doesn't let you chose which one you prefer, it returns text/html no matter what you pass as $supported. This may or may not be sufficient for you. see my other answer(s).

提交回复
热议问题