GithubHelp home page GithubHelp logo

robtro / fetch Goto Github PK

View Code? Open in Web Editor NEW

This project forked from phpgt/fetch

0.0 1.0 0.0 342 KB

Asynchronous HTTP client with promises.

Home Page: https://www.php.gt/fetch

License: MIT License

PHP 100.00%

fetch's Introduction

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.

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.


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

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;

    echo "Name of first result: "
    	. $json->results[0]->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.

fetch's People

Contributors

g105b avatar dependabot-support avatar dependabot-preview[bot] avatar j4m3s avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.