Download the PHP package mnapoli/bof without Composer
On this page you can find all versions of the php package mnapoli/bof. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Informations about the package bof
The HTTP client for humans.
Why?
Bof is a HTTP client meant to be as user friendly as possible.
It makes the most classic use cases, such as downloading a file, interacting with a JSON API or submitting a form, as simple as possible.
Since Bof is based on Guzzle, more advanced use cases can be addressed by using Guzzle's methods directly.
To sum up, Bof:
- is user friendly
- avoids magic strings and arrays for configuration: instead it provides explicit, typed and documented methods that can be autocompleted by IDEs
- comes with sane defaults: JSON is supported natively, 4xx and 5xx responses throw exceptions, timeouts are short by default
- is PSR-7 compliant
Future plans:
- PSR-18 compliance (the HTTP client standard)
- resiliency mechanisms such as retry, backoff, etc.
Want a short illustration? Here is Bof compared to Guzzle:
Do we need a new HTTP client?
Probably not. If this client attracts interest, that may mean that our already popular HTTP clients could use a simpler API targeting the simple use cases. If you maintain a HTTP client and are interested, I would love to merge Bof into existing libraries. Open an issue!
Installation
Usage
Configuration
The Bof\Http
class is immutable.
Configuration is applied by calling withXxx()
methods which create a new object every time:
Remember that withXxx()
methods return a copy of the original client:
Thanks to that pattern, the same methods can be used to apply configuration only for a specific request:
Responses
Responses are PSR-7 compliant. They also provide methods to facilitate working with JSON responses:
The getData()
method will decode the JSON response.
All PSR-7 methods are also available:
Sending JSON data
Using the JSON methods, the data will automatically encoded to JSON. A Content-Type
header of application/json
will be added.
Sending form data
Data can also be sent as a application/x-www-form-urlencoded
POST request:
Exceptions
Invalid HTTP responses (status code 4xx or 5xx) will throw exceptions.
Headers
Timeouts
Timeouts are set at short values by default:
- 5 seconds for the request timeout
- 3 seconds for the HTTP connection timeout
You can set shorter or longer timeouts (or disable them by setting them at 0
):
Query string parameters
You can set query string parameters in the request's URI:
$response = $http->get('http://httpbin.org?foo=bar');
You can specify the query string parameters as an array:
Providing the option as an array will use PHP's http_build_query
function to format the query string.
And finally, you can provide the query request option as a string.
Proxy
Use withSingleProxy()
to specify a proxy for all protocols:
Use withMultipleProxies()
to specify a different proxy for HTTP and HTTPS, as well as a list of host names that should not be proxied to:
Note that you can provide proxy URLs that contain a scheme, username, and password. For example, http://username:[email protected]:10
.
Guzzle integration
Bof is based on Guzzle. You can even make it use your own Guzzle client, for example if you preconfigured it:
All versions of bof with dependencies
ext-json Version *
guzzlehttp/guzzle Version ^6.4
guzzlehttp/psr7 Version ^1.6
psr/http-message Version ^1.0