phpgt/fetch

Asynchronous HTTP client with promises for PHP 7 applications.

Maintainers

Details

github.com/phpgt/fetch

Source

Issues

Installs: 1 060

Dependents: 0

Suggesters: 0

Stars: 6

Watchers: 2

Forks: 2

v0.6.0 2018-12-09 20:18 UTC

README

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.

Build status Code quality Code coverage Current version PHP.Gt/Fetch documentation

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;
});