Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 72 lines (59 sloc) 2.706 kb
62bf674 Will Milton markdown switchover for travis bling.
winmillwill authored
1 [![Build Status](https://secure.travis-ci.org/winmillwill/BadFaith.png?branch=master)](http://travis-ci.org/winmillwill/BadFaith)
ed40227 Will Milton just a quick change to the readme
winmillwill authored
2
d70c311 Will Milton updating readme with ideas
winmillwill authored
3 In Brief
4 ==========
b6cfbb4 Angie Byron Update README.md
webchick authored
5 BadFaith is a content negotiation framework which uses the HTTP spec to map variants that the web site supplies to what the user agent says it can use, similar to Apache's mod_negotiation functionality.
6
7 For example, a handset in Hong Kong wants a different pic format, different compression, different language, different charset than a desktop browser in the US.
8
62bf674 Will Milton markdown switchover for travis bling.
winmillwill authored
9 BadFaith content (/kənˈtɛnt/) negotiation has no assumptions about your environment/framework: it takes an array of the Accept and Accept-\* request-header fields you care about, keyed by name, parses them, and tells you which of the supplied variants you should use. If you don't supply that, it assumes you want to use the $\_SERVER variable. If you want to use your framework request object, then you can naturally extend the class.
d70c311 Will Milton updating readme with ideas
winmillwill authored
10
11 The Ideal API
12 ==============
13
14 ```php
15 <?php
16 // If you wish to use $_SERVER
17 $negotiator = new \BadFaith\Negotiator();
18
19 // If that is not desirable, furnish these header fields some other way
20 $acceptHeaders = array(
21 'accept' => 'text/html;level=2;q=0.7,text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
22 'accept_encoding' => 'gzip,deflate,sdch',
23 'accept_language' => 'en-US,en;q=0.8',
24 'accept_charset' => 'ISO-8859-1,utf-8;q=0.7,*;q=0.3',
25 );
26 $negotiator = new \BadFaith\Negotiator($acceptHeaders);
27
28 //Get information on what that user-agent wants for one dimension of variation:
29 $best_encoding = $negotiator->getPreferred('encoding')
30 echo $best_encoding; //Outputs 'gzip'.
31
32 //Get what would suit the UA best in all dimensions:
33 $best_all = $negotatiator->getPreferred();
34 var_export($best_all)
35 //Outputs
36 array(
37 'accept' => 'text/html',
38 'accept_encoding' => 'gzip',
39 'accept_language' => 'en-US',
40 'accept_charset' => 'ISO-8859-1',
41 );
42
43 // Provide the information on the variants you have for the resource
44 // ...using a string:
45 $negotiator->variants['charset'] = "UTF-8,iso-8859-1;q=0.9,UTF-16;q=0.5"
46 // ...using an array:
47 $negotiator->variants['charset'] = array(
48 array(
49 'type' => 'UTF-8',
50 'q' => 1.0,
51 ),
52 array(
53 'type' => 'iso-8859-1',
54 'q' => 0.9,
55 ),
56 array(
57 'type' => 'UTF-16',
58 'q' => 0.5,
59 ),
60 );
61
62 // Use that information to do real Content Negotiation:
63 $negotiator->getBestVariant()
64
65 // Or, if you're in a hurry:
66 $negotiator new \BadFaith\Negotiator($headers, $variants);
67
68 // Supply your own negotiation algorithm on the fly if you like
69 $best = negotiator->getBestVariant('algoCallable');
70 ?>
71 ```
Something went wrong with that request. Please try again.