Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/phpgt/fetch
Asynchronous HTTP client with promises.
https://github.com/phpgt/fetch
curl curl-functions fetch http http-client phpgt promise promise-support promises
Last synced: 3 days ago
JSON representation
Asynchronous HTTP client with promises.
- Host: GitHub
- URL: https://github.com/phpgt/fetch
- Owner: PhpGt
- License: mit
- Created: 2016-09-28T10:08:31.000Z (about 8 years ago)
- Default Branch: master
- Last Pushed: 2024-09-24T13:17:14.000Z (3 months ago)
- Last Synced: 2024-12-15T22:11:13.552Z (10 days ago)
- Topics: curl, curl-functions, fetch, http, http-client, phpgt, promise, promise-support, promises
- Language: PHP
- Homepage: https://www.php.gt/fetch
- Size: 383 KB
- Stars: 34
- Watchers: 4
- Forks: 9
- Open Issues: 11
-
Metadata Files:
- Readme: README.md
- Contributing: CONTRIBUTING.md
- Funding: .github/FUNDING.yml
Awesome Lists containing this project
README
# Asynchronous HTTP client with promises.
Asynchronous HTTP client, implementation of the [Fetch Standard][fetch-standard] which defines requests, responses, and the process that binds them: _fetching_.
See also, the [JavaScript implementation][fetch-js] that ships as standard in all modern browsers.
***
## Example usage: compute multiple HTTP requests in parallel, using `fetch`
```php
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;
});
```For more extensive examples, check out the code in the [example directory](/example).
[fetch-standard]: https://fetch.spec.whatwg.org/
[fetch-js]: https://developer.mozilla.org/en/docs/Web/API/Fetch_API