https://github.com/zelenin/http-client
PSR-18 compatible HTTP client with middleware support
https://github.com/zelenin/http-client
client http php psr-7
Last synced: about 1 month ago
JSON representation
PSR-18 compatible HTTP client with middleware support
- Host: GitHub
- URL: https://github.com/zelenin/http-client
- Owner: zelenin
- License: mit
- Created: 2017-01-11T18:09:11.000Z (over 8 years ago)
- Default Branch: master
- Last Pushed: 2019-07-06T21:01:19.000Z (almost 6 years ago)
- Last Synced: 2024-08-09T22:53:02.489Z (9 months ago)
- Topics: client, http, php, psr-7
- Language: PHP
- Homepage:
- Size: 46.9 KB
- Stars: 33
- Watchers: 4
- Forks: 1
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# HTTP client [](https://travis-ci.org/zelenin/http-client) [](https://coveralls.io/github/zelenin/http-client?branch=master)
[PSR-18](http://www.php-fig.org/psr/psr-18/) compatible low-level HTTP client with middleware support.
## Installation
### Composer
The preferred way to install this extension is through [Composer](http://getcomposer.org/).
Either run
```
php composer.phar require zelenin/http-client "~4.0.0"
```or add
```
"zelenin/http-client": "~4.0.0"
```to the ```require``` section of your ```composer.json```
## Usage
```php
use Zelenin\HttpClient\ClientFactory;
use Zend\Diactoros\Request;
use Zend\Diactoros\Uri;$client = (new ClientFactory())->create();
$request = new Request(new Uri('https://example.com/'), 'GET');
$response = $client->sendRequest($request);
```### Full example with middleware stack
```php
use Zelenin\HttpClient\Middleware\Cookie\CookieRequest;
use Zelenin\HttpClient\Middleware\Cookie\CookieResponse;
use Zelenin\HttpClient\Middleware\Cookie\FileStorage;
use Zelenin\HttpClient\Middleware\Deflate;
use Zelenin\HttpClient\Middleware\UserAgent;
use Zelenin\HttpClient\MiddlewareClient;
use Zelenin\HttpClient\RequestConfig;
use Zelenin\HttpClient\Transport\CurlTransport;
use Zend\Diactoros\Request;
use Zend\Diactoros\ResponseFactory;
use Zend\Diactoros\StreamFactory;
use Zend\Diactoros\Uri;$streamFactory = new StreamFactory();
$responseFactory = new ResponseFactory();$cookieStorage = new FileStorage('/tmp/http-client/cookies.storage');
$client = new MiddlewareClient([
new CookieRequest($cookieStorage),
new UserAgent(sprintf('HttpClient PHP/%s', PHP_VERSION)),
new Deflate($streamFactory),
new CookieResponse($cookieStorage),
new CurlTransport($streamFactory, $responseFactory, new RequestConfig()),
], $responseFactory);$request = new Request(new Uri('https://example.com/'), 'GET');
$response = $client->sendRequest($request);
```## Author
[Aleksandr Zelenin](https://github.com/zelenin/), e-mail: [[email protected]](mailto:[email protected])