Skip to content

Commit 3bcdeb5

Browse files
committed
:octocat:
1 parent 51df86b commit 3bcdeb5

File tree

1 file changed

+86
-40
lines changed

1 file changed

+86
-40
lines changed

README.md

Lines changed: 86 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -52,11 +52,91 @@ Download the desired version of the package from [master](https://github.com/chi
5252

5353
Profit!
5454

55-
## Usage
55+
## API
56+
57+
### [PSR-7](https://www.php-fig.org/psr/psr-7/) Message helpers
58+
These static methods can be found in the `chillerlan\HTTP\Psr7` namespace, along with implementations for each of the PSR-7 interfaces:
59+
60+
function | description
61+
---------|------------
62+
`normalize_request_headers(array $headers)` |
63+
`r_rawurlencode($data)` | recursive rawurlencode, accepts a string or an array as input
64+
`build_http_query(array $params, bool $urlencode = null, string $delimiter = null, string $enclosure = null)` | see [abraham/twitteroauth](https://github.com/abraham/twitteroauth/blob/master/src/Util.php#L82)
65+
`clean_query_params(iterable $params, int $bool_cast = null, bool $remove_empty = null)` | clean an array of parameters for URL queries (or JSON output etc.) using the following cast formats:<br>`BOOLEANS_AS_BOOL` - bool types will be left untouched (default)<br>`BOOLEANS_AS_INT` - cast to integer `1` and `0`<br>`BOOLEANS_AS_STRING` - a string value `"true"` and `"false"`<br>`BOOLEANS_AS_INT_STRING` - integer values, but as string, `"1"` and `"0"`
66+
`merge_query(string $uri, array $query)` | merges an array of query parameters into an URL query string
67+
`normalize_files(array $files)` |
68+
`create_uploaded_file_from_spec(array $value)` |
69+
`normalize_nested_file_spec(array $files = [])` |
70+
`get_json(ResponseInterface $response, bool $assoc = null)` |
71+
`get_xml(ResponseInterface $response)` |
72+
`message_to_string(MessageInterface $message)` | returns the string representation of a `MessageInterface`
73+
`decompress_content(MessageInterface $message)` | decompresses the message content according to the `Content-Encoding` header and returns the decompressed data
74+
75+
### [PSR-15](https://www.php-fig.org/psr/psr-15/) Request handlers and middleware
76+
These classes can be found in the `chillerlan\HTTP\Psr15` namespace:
77+
78+
class | PSR-15 type | description
79+
------|-------------|------------
80+
`EmptyResponseHandler` | `RequestHandlerInterface` |
81+
`QueueRunner` | `RequestHandlerInterface` | utilized by `QueueRequestHandler`
82+
`QueueRequestHandler` | `RequestHandlerInterface`, `MiddlewareInterface` |
83+
`PriorityQueueRequestHandler` | `RequestHandlerInterface`, `MiddlewareInterface` | extends `QueueRequestHandler`
84+
`PriorityMiddleware` | `MiddlewareInterface` | implements `PriorityMiddlewareInterface`
85+
86+
#### QueueRequestHandler example
5687

57-
### [`HTTPClientInterface`](https://github.com/chillerlan/php-httpinterface/blob/master/src/HTTPClientInterface.php)
58-
A `HTTPClientInterface` is usually invoked with a [`HTTPOptions`](https://github.com/chillerlan/php-httpinterface/blob/master/src/HTTPOptions.php) object as the first (optional) parameter,
59-
and - depending on the client - followed by one or more optional [PSR-17](https://www.php-fig.org/psr/psr-17/) message factories.
88+
```php
89+
// an iterable that contains several PSR-15 MiddlewareInterfaces
90+
$middlewareStack = [
91+
// ...
92+
];
93+
94+
// Fallback handler, using a PSR-17 ResponseFactory:
95+
$fallbackHandler = new EmptyResponseHandler($responseFactoryInterface, 200);
96+
97+
// Create request handler instance:
98+
$handler = new QueueRequestHandler($middlewareStack, $fallbackHandler);
99+
100+
// manually add a middleware
101+
$handler->add($middlewareInterface);
102+
103+
// execute it:
104+
$response = $handler->handle($serverRequestInterface);
105+
```
106+
The `PriorityQueueRequestHandler` works similar, with the difference that it also accepts `PriorityMiddlewareInterface` in the middleware stack, which allows you to specify a priority to control the order of execution.
107+
108+
### [PSR-17](https://www.php-fig.org/psr/psr-17/) Factory helpers
109+
These static methods can be found in the `chillerlan\HTTP\Psr17` namespace, along with implementations for each of the PSR-17 interfaces:
110+
111+
function | description
112+
---------|------------
113+
`create_server_request_from_globals()` | creates a PSR-7 `ServerRequestInterface` object that is populated with the GPCS superglobals
114+
`create_uri_from_globals()` | creates a PSR-7 `UriInterface` object that is populated with values from `$_SERVER`
115+
`create_stream(string $content = '')` | creates a PSR-7 `StreamInterface` object from a string
116+
`create_stream_from_input($in = null)` | creates a PSR-7 `StreamInterface` object from guessed input (string/scalar, resource, object)
117+
118+
### [PSR-18](https://www.php-fig.org/psr/psr-18/) HTTP Clients
119+
These classes can be found in the `chillerlan\HTTP\Psr18` namespace:
120+
121+
class | description
122+
------|------------
123+
`CurlClient` | a native cURL client
124+
`StreamClient` | a client that uses PHP's stream methods (still requires cURL)
125+
`URLExtractor` | a client that resolves shortened links (such as `t.co` or `goo.gl`) and returns the response for the last (deepest) URL
126+
`LoggingClient` | a logger client that wraps another `ClientInterface` and utilizes a `LoggerInterface` to log the request and response objects
127+
128+
The namespace `chillerlan\HTTP\CurlUtils` contains several classes related to `CurlClient`
129+
130+
class | description
131+
------|------------
132+
`CurlHandle` | used in `CurlClient` and `CurlMultiClient`
133+
`CurlMultiClient` | a `curl_multi` / "[Rolling Curl](http://www.onlineaspect.com/2009/01/26/how-to-use-curl_multi-without-blocking/)" implementation
134+
`MultiResponseHandlerInterface` | the response handler for `CurlMultiClient`
135+
136+
#### HTTP client example
137+
The built-in HTTP clients are usually invoked with a [`HTTPOptions`](https://github.com/chillerlan/php-httpinterface/blob/master/src/HTTPOptions.php) object as the first (optional) parameter,
138+
and - depending on the client - followed by one or more optional [PSR-17](https://www.php-fig.org/psr/psr-17/) message factories and a
139+
PSR-3 `LoggerInterface`.
60140
```php
61141
$options = new HTTPOptions([
62142
'ca_info' => '/path/to/cacert.pem',
@@ -65,46 +145,12 @@ $options = new HTTPOptions([
65145

66146
$http = new CurlClient($options, $myResponseFactory);
67147
```
68-
You can now fire a request via the implemented [PSR-18](https://www.php-fig.org/psr/psr-18/) method `ClientInterface::sendRequest()`,
69-
using an existing [PSR-7](https://www.php-fig.org/psr/psr-7/) `RequestInterface`...
148+
You can now fire a request via the implemented PSR-18 method `ClientInterface::sendRequest()`,
149+
using an existing PSR-7 `RequestInterface` and expect a PSR-7 `ResponseInterface`.
70150
```php
71151
use chillerlan\HTTP\Psr7\Request;
72152

73153
$request = new Request('GET', 'https://www.example.com?foo=bar');
74154

75155
$http->sendRequest($request);
76156
```
77-
...or you can use the `HTTPClientInterface::request()` method, which creates a new request using the provided (if any) factories.
78-
The `HTTPClientInterface` also provides constants for the HTTP methods via the [`RequestMethodInterface`](https://github.com/php-fig/http-message-util/blob/master/src/RequestMethodInterface.php).
79-
```php
80-
$http->request('https://www.example.com', $http::METHOD_GET, ['foo' => 'bar']);
81-
```
82-
Both methods will return a PSR-7 `ResponseInterface`.
83-
84-
### [PSR-7](https://www.php-fig.org/psr/psr-7/) Message helpers
85-
These static methods can be found in the `chillerlan\HTTP\Psr7` namespace:
86-
87-
- `normalize_request_headers(array $headers)`
88-
- `r_rawurlencode($data)` - recursive rawurlencode, accepts a string or an array as input
89-
- `build_http_query(array $params, bool $urlencode = null, string $delimiter = null, string $enclosure = null)` - see [abraham/twitteroauth](https://github.com/abraham/twitteroauth/blob/master/src/Util.php#L82)
90-
- `clean_query_params(iterable $params, int $bool_cast = null, bool $remove_empty = null)` - clean an array of parameters for URL queries (or JSON output etc.) using the following cast formats:
91-
- `BOOLEANS_AS_BOOL` - bool types will be left untouched (default)
92-
- `BOOLEANS_AS_INT` - cast to integer `1` and `0`
93-
- `BOOLEANS_AS_STRING` - a string value `"true"` and `"false"`
94-
- `BOOLEANS_AS_INT_STRING` - integer values, but as string, `"1"` and `"0"`
95-
- `merge_query(string $uri, array $query)` - merges an array of parameters into an URL query string
96-
- `normalize_files(array $files)`
97-
- `create_uploaded_file_from_spec(array $value)`
98-
- `normalize_nested_file_spec(array $files = [])`
99-
- `get_json(ResponseInterface $response, bool $assoc = null)`
100-
- `get_xml(ResponseInterface $response)`
101-
- `message_to_string(MessageInterface $message)` - returns the string representation of a `MessageInterface`
102-
- `decompress_content(MessageInterface $message)` - decompresses the message content according to the `Content-Encoding` header and returns the decompressed data
103-
104-
### [PSR-17](https://www.php-fig.org/psr/psr-17/) Factory helpers
105-
These static methods can be found in the `chillerlan\HTTP\Psr17` namespace:
106-
107-
- `create_server_request_from_globals()` - creates a PSR-7 `ServerRequestInterface` object that is populated with the GPCS superglobals.
108-
- `create_uri_from_globals()` - creates a PSR-7 `UriInterface` object that is populated with values from `$_SERVER`.
109-
- `create_stream(string $content = '')` - creates a PSR-7 `StreamInterface` object from a string.
110-
- `create_stream_from_input($in = null)` - creates a PSR-7 `StreamInterface` object from guessed input (string/scalar, file path, resource, object)

0 commit comments

Comments
 (0)