How to select content type from HTTP Accept header in PHP

后端 未结 7 1033
[愿得一人]
[愿得一人] 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:27

    Client may accept a list of mime-types in the response. In the other hand the order of the response is very important for client side. PHP Pear HTTP2 is the best to deal with language, charset, and mimetypes.

    $http = new HTTP2();
    $supportedTypes = array(
        'text/html',
        'application/json'
    );
    
    $type = $http->negotiateMimeType($supportedTypes, false);
    if ($type === false) {
        header('HTTP/1.1 406 Not Acceptable');
        echo "You don't want any of the content types I have to offer\n";
    } else {
        echo 'I\'d give you data of type: ' . $type . "\n";
    }
    

    Here is a good tutorial: https://cweiske.de/tagebuch/php-http-negotiation.htm

提交回复
热议问题