Gary Court

  • Home
  • About
  • Blog
  • Contact

Blog » Post

« Developing a Wordpress Theme Poplars »

Proper HTTP Content Negotiation in PHP

Last summer, I was looking into a quick PHP script that does HTTP content negotiation for a small project I was working on. After searching around Google for several hours, I found several scripts that determined if a browser supported proper XHTML mime-types or not, but nothing for proper HTTP content negotiation. So I decided to write one.

HTTP_Negotiate is written for PHP4 and uses the the content negotiation algorithm specified in draft-ietf-http-v11-spec-00. This function is also based off of the libwww-perl library HTTP::Negotiate.

Please note that I do not confirm the accuracy of this code. Though I have tried my best, there may be bugs within it. If you happen to find any, please let me know! Enjoy!

Usage:

  1. Download the script into your project directory and extract it.
  2. Include the script into your project.
  3. Call using HTTP_Negotiate::choose().

API:

HTTP_Negotiate::choose($variants, $request_headers)

array $variants
Array of array of strings which contain the supported parameters of each variant. (supported keys: id, qs, type, encoding, charset, language, size)

Example:

$variants = array(
    array(
      id => 'var1',
      qs => 1.000,
      type => 'text/html',
      encoding => null,
      charset => 'iso-8859-1',
      language => 'en',
      size => 3000
    ),
    array(
      id => 'var2',
      qs => 1.000,
      type => 'application/xhtml+xml',
      encoding => 'gzip',
      charset => 'iso-8859-1',
      language => 'en',
      size => 1500
    ),
  );
    
array $request_headers
Array of strings which contain the request header (supported keys: HTTP_ACCEPT, HTTP_ACCEPT_LANGUAGE, HTTP_ACCEPT_CHARSET, HTTP_ACCEPT_ENCODING).
Unless you are using a custom request header, leave this parameter null and the function will use the header from $_SERVER.

Example:

$request_headers = array(
  HTTP_ACCEPT => 'text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5',
  HTTP_ACCEPT_LANGUAGE => 'en-us,en;q=0.5',
  HTTP_ACCEPT_CHARSET => 'ISO-8859-1,utf-8;q=0.7,*;q=0.7', 
  HTTP_ACCEPT_ENCODING => 'gzip,deflate'
);
    

More information on accept headers can be found at http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html.

Returns: array
Returns the acceptable variants (from $variants) based on the request headers. May return more then one acceptable variant (in original order) or may return null (if no acceptable variant was found).

Example:

$results = HTTP_Negotiate::choose($variants, $request_headers);
assertTrue(count($results) == 1 && $results[0]['id'] == 'var2');
    

To Do:

  • Support for parameters in variant ‘type’.

Click here to view script.
Click here to download script.

Leave a Reply

Information

  • Author

    Gary Court
  • Posted

    Monday, January 9th, 2006 at 12:09 pm
  • Category

    • PHP
  • Tags

    • PHP
    • HTTP
    • content negotiation
    • script
    • XHTML
    • mime
    • api
    • code
  • Response

    • Comment
    • Trackback
  • Syndication

    • RSS 2.0 Comments
  • Related Posts

    • No related posts
CourtNET

© 2005 Gary Court. All rights reserved. | Valid: XHTML CSS | XFN | Powered by WordPress & Gallery 2.