-
-
Notifications
You must be signed in to change notification settings - Fork 8
Home
The Fetch API is a web standard for making HTTP requests in a promise-based way. If you have used fetch() in the browser, the core idea will already feel familiar: make a request, work with a Response, and choose whether to wait for one request or run many at the same time.
This project brings that workflow to PHP. Behind the scenes it uses cURL (specifically curl_multi for concurrency), but the public API is designed to feel as close to the browser's fetch API as possible.
There is no shortage of HTTP clients in PHP, and if you already have a preferred tool there is no need to replace it. The purpose of this project is to bring the consistency of web standards to your server-side code.
PHP.GT focuses on bringing familiar web standards to server-side PHP. With Fetch, we can use a very similar mental model on both the client and the server:
- create a request
- receive a
Response - process the body as text, JSON or a blob
- run multiple requests concurrently
- optionally cancel a request with an abort signal
That makes code easier to move between environments, and easier to reason about when we are building systems that span browser and server code.
At the centre of the library is the GT\Fetch\Http object. It provides:
-
fetch()for non-blocking HTTP requests that return a Promise -
awaitFetch()for a blocking style when we just want theResponse -
wait()to complete all queued requests -
all()to run all queued requests and resolve when everything has finished
Requests may be configured with familiar fetch-style options such as:
methodheadersbodyredirectreferrerintegritykeepalivesignal
There is also support for passing a PSR-7 RequestInterface directly to fetch(), which makes interop straightforward if the request has already been built elsewhere in your application.
If you are new to the library, start with the overview. From there we will look at constructing Http objects, making requests, choosing how to wait for them, and using the request options that are currently implemented.
When you want to see complete scripts, take a look at the usage examples page, which mirrors the runnable files in the repository's example/ directory.
PHP.GT/Fetch is a separately maintained component of PHP.GT/WebEngine.