Class HTTP_Accept

Description

HTTP_Accept class for dealing with the HTTP 'Accept' header

This class is intended to be used to parse the HTTP Accept header into usable information and provide a simple API for dealing with that information.

The parsing of this class is designed to follow RFC 2616 to the letter, any deviations from that standard are bugs and should be reported to the maintainer.

Often the class will be used very simply as

  1.  <?php
  2.  $accept new HTTP_Accept($_SERVER['HTTP_ACCEPT']);
  3.  if ($accept->getQuality("image/png"$accept->getQuality("image/jpeg"))
  4.      // Send PNG image
  5.  else
  6.      // Send JPEG image
  7.  ?>

However, for browsers which do not accurately describe their preferences, it may be necessary to check if a MIME Type is explicitly listed in their Accept header, in addition to being preferred to another type

  1.  <?php
  2.   if ($accept->isMatchExact("application/xhtml+xml"))
  3.       // Client specifically asked for this type at some quality level
  4.  ?>

Located in /HTTP_Accept.php (line 60)


	
			
Method Summary
object HTTP_Accept HTTP_Accept (string $acceptstring)
void addType (string $mimetype, [double $quality = 1], [array $params = array()], [array $extensions = array()])
array getExtensionSets (string $mimetype)
array getParameterSets (string $mimetype)
double getQuality (string $mimetype, [array $params = array()], [array $extensions = array()])
array getTypes ()
boolean isMatchExact (string $mimetype, [array $params = array()], [array $extensions = array()])
void removeType (string $mimetype, [array $params = array()], [array $extensions = array()])
string __toString ()
Methods
Constructor HTTP_Accept (line 110)

Constructs a new HTTP_Accept object

Initializes the HTTP_Accept class with a given accept string or creates a new (empty) HTTP_Accept object if no string is given

Note: The behavior is a little strange here to accomodate missing headers (to be interpreted as accept all) as well as new empty objects which should accept nothing. This means that HTTP_Accept("") will be different than HTTP_Accept()

  • access: public
object HTTP_Accept HTTP_Accept (string $acceptstring)
  • string $acceptstring: The value of an Accept: header Will often be $_SERVER['HTTP_ACCEPT'] Note: If get_magic_quotes_gpc is on, run stripslashes() on the string first
addType (line 465)

Adds a type to the set of accepted types

  • access: public
void addType (string $mimetype, [double $quality = 1], [array $params = array()], [array $extensions = array()])
  • string $mimetype: The MIME Type to add (e.g. "text/html")
  • double $quality: The quality value for the given MIME Type Quality values are in the range [0,1] where
    1. means "not accepted" and 1 is
    "perfect quality".
  • array $params: Parameters of the type to add (e.g. [level => 4])
  • array $extensions: Extension parameters of the type to add
getExtensionSets (line 428)

Gets the extension sets for a given mime type, sorted by quality.

Only extension sets where the parameter set is empty will be returned.

  • return: list of sets of name=>value extension pairs in decreasing order of quality factor
  • access: public
array getExtensionSets (string $mimetype)
  • string $mimetype: The MIME Type to query ("text/html")
getParameterSets (line 394)

Gets the parameter sets for a given mime type, sorted by quality.

Only parameter sets where the extensions set is empty will be returned.

  • return: list of sets of name=>value parameter pairs in decreasing order of quality factor
  • access: public
array getParameterSets (string $mimetype)
  • string $mimetype: The MIME Type to query ("text/html")
getQuality (line 276)

Gets the accepted quality factor for a given MIME Type

Note: If there are multiple best matches (e.g. "text/html;level=4;charset=utf-8" matching both "text/html;level=4" and "text/html;charset=utf-8"), it returns the lowest quality factor as a conservative estimate. Further, if the ambiguity is between parameters and extensions (e.g. "text/html;level=4;q=1;ext=foo" matching both "text/html;level=4" and "text/html;q=1;ext=foo") the parameters take precidence.

Usage Note: If the quality factor for all supported media types is 0, RFC 2616 specifies that applications SHOULD send an HTTP 406 (not acceptable) response.

  • return: the quality value for the given MIME Type Quality values are in the range [0,1] where 0 means "not accepted" and 1 is "perfect quality".
  • access: public
double getQuality (string $mimetype, [array $params = array()], [array $extensions = array()])
  • string $mimetype: The MIME Type to query ("text/html")
  • array $params: Parameters of Type to query ([level => 4])
  • array $extensions: Extension parameters to query
getTypes (line 355)

Gets a list of all MIME Types explicitly accepted, sorted by quality

  • return: list of MIME Types explicitly accepted, sorted in decreasing order of quality factor
  • access: public
array getTypes ()
isMatchExact (line 333)

Determines if there is an exact match for the specified MIME Type

  • return: true if there is an exact match to the given values, false otherwise.
  • access: public
boolean isMatchExact (string $mimetype, [array $params = array()], [array $extensions = array()])
  • string $mimetype: The MIME Type to query (e.g. "text/html")
  • array $params: Parameters of Type to query (e.g. [level => 4])
  • array $extensions: Extension parameters to query
removeType (line 502)

Removes a type from the set of accepted types

  • access: public
void removeType (string $mimetype, [array $params = array()], [array $extensions = array()])
  • string $mimetype: The MIME Type to remove (e.g. "text/html")
  • array $params: Parameters of the type to remove (e.g. [level => 4])
  • array $extensions: Extension parameters of the type to remove
__toString (line 530)

Gets a string representation suitable for use in an HTTP Accept header

  • return: a string representation of this object
  • access: public
string __toString ()

Documentation generated on Sat, 06 Oct 2007 15:07:01 -0400 by phpDocumentor 1.4.0