Skip to content

Commit 7aa258b

Browse files
committed
:octocat:
1 parent 1d40911 commit 7aa258b

File tree

2 files changed

+139
-1
lines changed

2 files changed

+139
-1
lines changed

README.md

+136-1
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@ A [PSR-7](https://www.php-fig.org/psr/psr-7/)/[PSR-17](https://www.php-fig.org/p
2828

2929
# Documentation
3030

31-
See [the wiki](https://github.com/chillerlan/php-httpinterface/wiki) for advanced documentation.
3231
An API documentation created with [phpDocumentor](https://www.phpdoc.org/) can be found at https://chillerlan.github.io/php-httpinterface/ (WIP).
3332

3433

@@ -59,6 +58,142 @@ In case you want to keep using `dev-main`, specify the hash of a commit to avoid
5958
Profit!
6059

6160

61+
## Quickstart
62+
63+
The HTTP clients `CurlClient` and `StreamClient` are invoked with a `ResponseFactoryInterface` instance
64+
as the first parameter, followed by optional `HTTPOptions` and PSR-3 `LoggerInterface` instances.
65+
You can then send a request via the implemented PSR-18 method `ClientInterface::sendRequest()`,
66+
using a PSR-7 `RequestInterface` and expect a PSR-7 `ResponseInterface`.
67+
68+
### `CurlClient`, `StreamClient`
69+
70+
```php
71+
$options = new HTTPOptions;
72+
$options->ca_info = '/path/to/cacert.pem';
73+
$options->user_agent = 'my cool user agent 1.0';
74+
75+
$httpClient = new CurlClient($responseFactory, $options, $logger);
76+
$request = $requestFactory->createRequest('GET', 'https://www.example.com?foo=bar');
77+
78+
$httpClient->sendRequest($request);
79+
```
80+
81+
82+
### `CurlMultiClient`
83+
84+
The `CurlMultiClient` client implements asynchronous multi requests (["rolling-curl"](https://code.google.com/archive/p/rolling-curl/)).
85+
It needs a `MultiResponseHandlerInterface` that parses the incoming responses, the callback may return a failed request to the stack:
86+
87+
```php
88+
$handler = new class () implements MultiResponseHandlerInterface{
89+
90+
public function handleResponse(
91+
ResponseInterface $response, // the incoming response
92+
RequestInterface $request, // the corresponding request
93+
int $id, // the request id
94+
array $curl_info , // the curl_getinfo() result for this request
95+
):RequestInterface|null{
96+
97+
if($response->getStatusCode() !== 200){
98+
// return the failed request back to the stack
99+
return $request;
100+
}
101+
102+
try{
103+
$body = $response->getBody();
104+
105+
// the response body is empty for some reason, we pretend that's fine and exit
106+
if($body->getSize() === 0){
107+
return null;
108+
}
109+
110+
// parse the response body, store the result etc.
111+
$data = $body->getContents();
112+
113+
// save data to file, database or whatever...
114+
// ...
115+
116+
}
117+
catch(Throwable){
118+
// something went wrong, return the request to the stack for another try
119+
return $request;
120+
}
121+
122+
// everything ok, nothing to return
123+
return null;
124+
}
125+
126+
};
127+
```
128+
129+
You can then invoke the multi request client - the `MultiResponseHandlerInterface` and `ResponseFactoryInterface` are mandatory,
130+
`HTTPOptions` and `LoggerInterface` are optional:
131+
132+
```php
133+
$options = new HTTPOptions;
134+
$options->ca_info = '/path/to/cacert.pem';
135+
$options->user_agent = 'my cool user agent 1.0';
136+
$options->sleep = 750000; // microseconds, see usleep()
137+
$options->window_size = 5;
138+
$options->retries = 1;
139+
140+
$multiClient = new CurlMultiClient($handler, $responseFactory, $options, $logger);
141+
142+
// create and add the requests
143+
foreach(['..', '...', '....'] as $item){
144+
$multiClient->addRequest($factory->createRequest('GET', $endpoint.'/'.$item));
145+
}
146+
147+
// process the queue
148+
$multiClient->process();
149+
```
150+
151+
152+
### `URLExtractor`
153+
154+
The `URLExtractor` wraps a PSR-18 `ClientInterface` to extract and follow shortened URLs to their original location.
155+
156+
```php
157+
$urlExtractor = new URLExtractor($httpClient, $responseFactory);
158+
159+
$request = $factory->createRequest('GET', 'https://t.co/ZSS6nVOcVp');
160+
161+
$urlExtractor->sendRequest($request); // -> response from the final location
162+
163+
// you can retrieve an array with all followed locations afterwards
164+
$responses = $this->http->getResponses(); // -> ResponseInterface[]
165+
166+
// if you just want the URL of the final location, you can use the extract method:
167+
$url = $this->http->extract('https://t.co/ZSS6nVOcVp'); // -> https://api.guildwars2.com/v2/build
168+
```
169+
170+
171+
### `LoggingClient`
172+
173+
The `LoggingClient` wraps a `ClientInterface` and outputs the HTTP messages in a readable way through a `LoggerInterface` (do NOT use in production!).
174+
175+
```php
176+
$loggingClient = new LoggingClient($httpClient, $logger);
177+
178+
$loggingClient->sendRequest($request); // -> log to output given via logger
179+
```
180+
181+
182+
### Auto generated API documentation
183+
184+
The API documentation can be auto generated with [phpDocumentor](https://www.phpdoc.org/).
185+
There is an [online version available](https://chillerlan.github.io/php-httpinterface/) via the [gh-pages branch](https://github.com/chillerlan/php-httpinterface/tree/gh-pages) that is [automatically deployed](https://github.com/chillerlan/php-httpinterface/deployments) on each push to main.
186+
187+
Locally created docs will appear in the directory `.build/phpdocs/`. If you'd like to create local docs, please follow these steps:
188+
189+
- [download phpDocumentor](https://github.com/phpDocumentor/phpDocumentor/releases) v3+ as .phar archive
190+
- run it in the repository root directory:
191+
- on Windows `c:\path\to\php.exe c:\path\to\phpDocumentor.phar --config=phpdoc.xml`
192+
- on Linux just `php /path/to/phpDocumentor.phar --config=phpdoc.xml`
193+
- open [index.html](./.build/phpdocs/index.html) in a browser
194+
- profit!
195+
196+
62197
## Disclaimer
63198

64199
Use at your own risk!

composer.json

+3
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,9 @@
4545
"phpunit/phpunit": "^10.5",
4646
"squizlabs/php_codesniffer": "^3.9"
4747
},
48+
"suggest": {
49+
"chillerlan/php-oauth": "A PSR-7 OAuth client/handler that also acts as PSR-18 HTTP client"
50+
},
4851
"autoload": {
4952
"psr-4": {
5053
"chillerlan\\HTTP\\": "src/"

0 commit comments

Comments
 (0)