s/NOTE/Note/
[mdref/mdref-http] / http / Env / negotiateContentType.md
1 # static string http\Env::negotiateContentType(array $supported[, array &$result)
2
3 Negotiate the client's preferred MIME content type.
4
5 > **Note:** The first elemement of $supported content types serves as a default if no content-type matches.
6
7 ## Params:
8
9 * array $supported
10 List of supported MIME content types.
11 * Optional reference array &$result
12 Out parameter recording negotiation results.
13
14 ## Returns:
15
16 * NULL, if negotiation fails.
17 * string, the negotiated content type.
18
19 ## Example:
20
21 A client indicates his accepted MIME content types by sending an Accept
22 header. The static http\Env class provides a facility to negotiate the
23 client's preferred content type:
24
25 <?php
26 $_SERVER["HTTP_ACCEPT"] = implode(",", array(
27 "text/html",
28 "text/plain",
29 "text/*;q=0.9",
30 "*/*;q=0.1",
31 "application/xml;q=0"
32 ));
33 $supported = array(
34 "text/html",
35 "text/plain",
36 "application/json",
37 "application/xml"
38 );
39 $preferred = http\Env::negotiateContentType($supported, $ranking);
40 var_dump($preferred, $ranking);
41 ?>
42
43 Running this script should give the following output:
44
45 string(9) "text/html"
46 array(3) {
47 'text/html' =>
48 float(0.99)
49 'text/plain' =>
50 float(0.98)
51 'application/json' =>
52 float(0.1)
53 }
54
55
56