Asynchronous HTTP client with promises.
Asynchronous HTTP client, PSR-7 compatible implementation of the Fetch Standard which defines requests, responses, and the process that binds them: fetching.
See also, the JavaScript implementation that ships as standard in all modern browsers.
Example usage: compute multiple HTTP requests in parallel.
<?php
$http = new Gt\Fetch\Http();
$http->get("http://example.com/api/something.json")
->then(function($response) {
if($response->status !== 200) {
echo "Looks like there was a problem. Status code: "
. $response->status . PHP_EOL;
return;
}
return $response->json();
})
->then(function($json) {
echo "Got JSON result length "
. count($json->results)
. PHP_EOL;
echo "Name of first result: "
. $json->results[0]->name
. PHP_EOL;
});
$http->get("http://example.com/something.jpg")
->then(function($response) {
return $response->blob();
})
->then(function($blob) {
echo "Got JPG blob. Saving file." . PHP_EOL;
file_put_contents("/tmp/something.jpg", $blob);
});
$http->all()->then(function() {
echo "All HTTP calls have completed!" . PHP_EOL;
});