You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
FetchPHP is a PHP library that mimics the behavior of JavaScript’s `fetch` API using the powerful Guzzle HTTP client. FetchPHP supports both synchronous and asynchronous requests, and provides an easy-to-use, flexible API for making HTTP requests in PHP.
10
+
FetchPHP is a PHP library that mimics the behavior of JavaScript’s `fetch` API using the powerful Guzzle HTTP client. FetchPHP supports both synchronous and asynchronous requests and provides an easy-to-use, flexible API for making HTTP requests in PHP.
By default, the Guzzle HTTP client is instantiated every time the `fetch` or `fetchAsync` function is called. While this is fine for most cases, it can introduce some inefficiency if you're making frequent HTTP requests in your application.
34
-
35
-
#### **Mitigating Guzzle Client Reinstantiation**
36
-
37
-
You can mitigate the overhead of creating a new Guzzle client each time by passing a custom Guzzle client through the `options` parameter. This allows you to use a **singleton instance** of the client across multiple `fetch` requests.
34
+
By default, FetchPHP uses a single instance of the Guzzle client shared across all requests. However, you can provide your own Guzzle client through the `options` parameter of both `fetch` and `fetch_async`. This gives you full control over the client configuration, including base URI, headers, timeouts, and more.
38
35
39
36
### **How to Provide a Custom Guzzle Client**
40
37
@@ -55,7 +52,6 @@ $response = fetch('/todos/1', [
55
52
'client' => $client
56
53
]);
57
54
58
-
// The Guzzle client instance will now be reused across multiple fetch calls
59
55
$response2 = fetch('/todos/2', [
60
56
'client' => $client
61
57
]);
@@ -69,7 +65,7 @@ print_r($response2->json());
69
65
Passing a singleton Guzzle client is useful when:
70
66
71
67
- You're making many requests and want to avoid the overhead of creating a new client each time.
72
-
- You want to configure specific client-wide options (e.g., base URI, timeouts, headers) and use them across multiple requests.
68
+
- You want to configure specific client-wide options (e.g., base URI, timeouts, headers) and reuse them across multiple requests.
73
69
74
70
---
75
71
@@ -106,9 +102,9 @@ echo $response->statusText();
106
102
107
103
---
108
104
109
-
### **2. Asynchronous Requests with `fetchAsync`**
105
+
### **2. Asynchronous Requests with `fetch_async`**
110
106
111
-
The `fetchAsync` function returns a `PromiseInterface` object. You can use the `.then()` and `.wait()` methods to manage the asynchronous flow.
107
+
The `fetch_async` function returns a `PromiseInterface` object. You can use the `.then()` and `.wait()` methods to manage the asynchronous flow.
112
108
113
109
#### **Basic Asynchronous GET Request Example**
114
110
@@ -117,7 +113,7 @@ The `fetchAsync` function returns a `PromiseInterface` object. You can use the `
0 commit comments