phpgt / fetch
Asynchronous HTTP client with promises.
Fund package maintenance!
PhpGt
Installs: 10 231
Dependents: 0
Suggesters: 0
Security: 0
Stars: 29
Watchers: 3
Forks: 8
Open Issues: 17
Requires
- php: >=8.1
- ext-curl: *
- ext-json: *
- php-http/httplug: ^2.3
- phpgt/async: dev-master
- phpgt/curl: ^v3.0
- phpgt/http: ^v1.1
- phpgt/json: ^v1.1
- phpgt/promise: dev-master as v2.0.0
Requires (Dev)
- phpstan/phpstan: v1.8.0
- phpunit/phpunit: v9.5.21
Provides
- dev-master
- v2.x-dev
- v1.1.0
- v1.0.1
- v1.0.0
- v0.7.0
- v0.6.3
- v0.6.2
- v0.6.1
- v0.6.0
- v0.5.0
- v0.4.0
- v0.3.1
- v0.3.0
- v0.2.1
- v0.2.0
- v0.1.1
- v0.1.0
- v0.0.2
- v0.0.1
- dev-dependabot/composer/phpstan/phpstan-1.10.9
- dev-dependabot/composer/phpgt/curl-3.1.0
- dev-ci-improvements
- dev-dependabot/composer/phpunit/phpunit-10.0.19
- dev-http-client-keyword
- dev-dependabot/composer/phpstan/phpstan-1.10.8
- dev-dependabot/composer/phpgt/http-1.1.9
- dev-34-psr-18
- dev-54-json-improvements
- dev-circleci
This package is auto-updated.
Last update: 2023-03-31 02:57:33 UTC
README
Asynchronous HTTP client, PSR-7 compatible implementation of the Fetch Standard which defines requests, responses, and the process that binds them: fetching.
This repository provides a PHP-HTTP Client Implementation for standardised HTTP interoperability.
See also, the JavaScript implementation that ships as standard in all modern browsers.
Example usage: compute multiple HTTP requests in parallel, using fetch
<?php $http = new Gt\Fetch\Http(); // Rather than creating the request now, `fetch` returns a Promise, // for later resolution with the BodyResponse. $http->fetch("http://example.com/api/something.json") ->then(function(BodyResponse $response) { // The first Promise resolves as soon as a response is received, even before // the body's content has completed downloading. if(!$response->ok) { echo "Looks like there was a problem. Status code: " . $response->getStatusCode() . PHP_EOL; return null; } // Within this Promise callback, you have access to the body stream, but // to access the contents of the whole body, return a new Promise here: return $response->json(); }) ->then(function(Json $json) { // The second Promise resolves once the whole body has completed downloading. echo "Got JSON result length " . count($json->results) . PHP_EOL; // Notice that type-safe getters are available on all Json objects: echo "Name of first result: " . $json->results[0]->getString("name") . PHP_EOL; }); // A third request is made here to show a different type of body response: $http->fetch("http://example.com/something.jpg") ->then(function(BodyResponse $response) { return $response->blob(); }) ->then(function($blob) { echo "Got JPG blob. Saving file." . PHP_EOL; file_put_contents("/tmp/something.jpg", $blob); }); // Once all Promises are registered, all HTTP requests can be initiated in // parallel, with the callback function triggered when they are all complete. $http->all()->then(function() { echo "All HTTP calls have completed!" . PHP_EOL; });
Example usage: HTTPlug PHP-HTTP Client & Asynchronous Client
<?php $http = new Gt\Fetch\Http(); $slowRequest = new Request("GET", "http://slow.example.com"); $fastRequest = new Request("GET", "http://fast.example.com"); // Send the slow request asynchronously (returns a Http\Promise) $http->sendAsyncRequest($slowRequest) ->then(function(ResponseInterface $response) { echo $response->getBody(); }); // Perform fast request synchronously (block until response ready) $response = $http->sendRequest($fastRequest); // Wait for any asynchronous requests to be completed. $http->wait();
For more extensive examples, check out the code in the example directory.