|
1 | | -# Async HTTP Client |
2 | | -Asynchronous HTTP Client with morden PHP 8.1 or later |
| 1 | +# Async HTTP Client for PHP |
| 2 | + |
| 3 | +[](https://packagist.org/packages/codewithsushil/async-http-client) |
| 4 | +[](https://github.com/codewithsushil/async-http-client/actions) |
| 5 | + |
| 6 | +A fully async, non-blocking HTTP client built using `stream_socket_client` and `stream_select`. No cURL. No Guzzle. |
| 7 | + |
| 8 | +## Features |
| 9 | + |
| 10 | +- 🌀 Non-blocking requests using PHP streams |
| 11 | +- 🔁 Multiple async requests in parallel |
| 12 | +- 🔄 Retries and timeout support |
| 13 | +- 🧩 PSR-18 / PSR-7 compatible |
| 14 | +- ✅ GET, POST, PUT, PATCH, DELETE support |
| 15 | +- 📦 Composer-ready |
| 16 | + |
| 17 | +## Install |
| 18 | + |
| 19 | +```bash |
| 20 | +composer require codewithsushil/async-http-client |
| 21 | +``` |
| 22 | + |
| 23 | +## Example |
| 24 | + |
| 25 | +```php |
| 26 | +use AsyncHttp\Http\AsyncHttpClient; |
| 27 | + |
| 28 | +$client = new AsyncHttpClient(); |
| 29 | +foreach ($client->get('https://jsonplaceholder.typicode.com/posts/1') as $response) { |
| 30 | + echo $response->getBody(); |
| 31 | +} |
| 32 | +``` |
| 33 | + |
| 34 | +## Parallel Requests |
| 35 | + |
| 36 | +```php |
| 37 | +use AsyncHttp\Http\AsyncHttpClient; |
| 38 | +use AsyncHttp\Http\MultiAsyncHandler; |
| 39 | + |
| 40 | +$client = new AsyncHttpClient(); |
| 41 | +$multi = new MultiAsyncHandler(); |
| 42 | + |
| 43 | +$urls = [ |
| 44 | + 'https://jsonplaceholder.typicode.com/posts/1', |
| 45 | + 'https://jsonplaceholder.typicode.com/posts/2', |
| 46 | +]; |
| 47 | + |
| 48 | +foreach ($urls as $url) { |
| 49 | + $multi->add(fn() => $client->get($url), function($res) use ($url) { |
| 50 | + echo "[$url] => " . substr($res->getBody(), 0, 80) . "\n"; |
| 51 | + }); |
| 52 | +} |
| 53 | + |
| 54 | +$multi->run(); |
| 55 | +``` |
0 commit comments