https://github.com/ptlis/conneg
Content Negotiation for PHP
https://github.com/ptlis/conneg
conneg content-negotiation php
Last synced: 8 months ago
JSON representation
Content Negotiation for PHP
- Host: GitHub
- URL: https://github.com/ptlis/conneg
- Owner: ptlis
- License: mit
- Created: 2012-11-30T14:44:19.000Z (over 13 years ago)
- Default Branch: master
- Last Pushed: 2021-09-11T14:30:36.000Z (over 4 years ago)
- Last Synced: 2025-09-16T14:16:18.037Z (9 months ago)
- Topics: conneg, content-negotiation, php
- Language: PHP
- Homepage:
- Size: 792 KB
- Stars: 35
- Watchers: 5
- Forks: 2
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGES.md
- License: LICENSE
Awesome Lists containing this project
README
# ConNeg
Content Negotiation for PHP.
This framework-independent library provides tooling to allow you to support content negotiation in your applications.
Supports negotiation on the [Accept](http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.1), [Accept-Charset](http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.2), [Accept-Encoding](http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.3) and [Accept-Language](http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.4) fields in a HTTP header.
[](https://app.travis-ci.com/github/ptlis/conneg) [](https://scrutinizer-ci.com/g/ptlis/conneg/) [](https://scrutinizer-ci.com/g/ptlis/conneg/) [](https://github.com/ptlis/conneg/blob/master/LICENSE) [](https://packagist.org/packages/ptlis/conneg)
## Install
With composer:
```shell
$ composer require ptlis/conneg:~4.0.0
```
## Usage
### In a PSR-7 Project
If your application supports PSR-7 then the simplest way to get content negotiation is via the middlewares provided by [ptlis/psr7-conneg](https://github.com/ptlis/psr7-conneg).
### In non PSR-7 Projects
First create a Negotiation instance. This provides methods to perform negotiation on client and server preferences.
```php
use ptlis\ConNeg\Negotiation;
$negotiation = new Negotiation();
```
In most cases your application will only care about the best match, to get these we can use the ```*Best()``` methods.
For example, negotiation to decide whether to serve JSON or XML (preferring JSON) would look like:
```php
$bestMime = $negotiation->mimeBest(
$_SERVER['ACCEPT'],
'application/json;q=1,application/xml;q=0.75'
);
```
This will return a string representation of the best matching mime-type specified by the server's preferences, for example 'application/json'.
Negotiation of Language, Encoding & Charset can be done by using the appropriate method (languageBest, encodingBest & charsetBest respectively).
**Note:** server preferences a string-encoded as described [in the documentation](http://ptlis.github.io/conneg/basics.html#type-preference-encodings).
See the [detailed usage docs](http://ptlis.github.io/conneg/usage.html) for further (more complex) examples.
## Documentation
[Full Documentation](http://ptlis.github.io/conneg/)
## Integration
* PSR-7 via the [ptlis/psr7-conneg](https://github.com/ptlis/psr7-conneg) package, with middlewares supporting:
* [Zend Stratigility](https://github.com/zendframework/zend-stratigility)
* [Relay](https://github.com/relayphp/Relay.Relay)
* Symfony2 via the [ptlis/conneg-bundle](https://github.com/ptlis/conneg-bundle) Bundle.
## Contributing
You can contribute by submitting an Issue to the [issue tracker](https://github.com/ptlis/conneg/issues), improving the [documentation](https://github.com/ptlis/conneg/tree/gh-pages), integrating the library into your framework of choice or submitting a pull request. For pull requests i'd prefer that the code style and test coverage is maintained, but I am happy to work through any minor issues that may arise so that the request can be merged.
## TODO
* Time based negotiation? See RFC 7089