|
1 | 1 | <!-- markdownlint-disable no-inline-html --> |
2 | 2 | <p align="center"> |
3 | 3 | <br><br> |
4 | | - <img src="https://leaf-docs.netlify.app/images/logo.png" height="100"/> |
| 4 | + <img src="https://leafphp.dev/logo-circle.png" height="100"/> |
5 | 5 | <h1 align="center">Fetch</h1> |
6 | 6 | <br><br> |
7 | 7 | </p> |
8 | 8 |
|
9 | | -# Leaf Fetchs |
10 | | - |
11 | 9 | [](https://packagist.org/packages/leafs/leaf) |
12 | 10 | [](https://packagist.org/packages/leafs/leaf) |
13 | 11 | [](https://packagist.org/packages/leafs/leaf) |
14 | 12 |
|
15 | | -Clean, simple, developer friendly interface for making network requests with PHP. Fetch is based on curl and uses elements from Unirest PHP and an API that closely resembles Axios. All of these combined makes Fetch the best and simplest way to make PHP network requests. |
| 13 | +When building your applications, you will probably end up needing to call APIs or fetch data from external sources. Leaf provides a simple and easy way to do this using Fetch. Fetch provides a clean and modern interface for making network requests in PHP. It is inspired by JavaScript's Fetch API, Axios and uses elements from Unirest PHP. |
16 | 14 |
|
17 | 15 | ## fetch example |
18 | 16 |
|
19 | 17 | ```php |
20 | | -use function Leaf\fetch; |
21 | | - |
22 | | -$res = fetch("https://jsonplaceholder.typicode.com/todos/"); |
| 18 | +$res = fetch('https://jsonplaceholder.typicode.com/todos/'); |
23 | 19 |
|
24 | | -echo json_encode($res->data); |
| 20 | +// data returned is saved in the $data property just like axios |
| 21 | +response()->json($res->data); |
25 | 22 | ``` |
26 | 23 |
|
27 | | -You can also use the fetch class |
| 24 | +You can also use the entire fetch object to make requests: |
28 | 25 |
|
29 | 26 | ```php |
30 | | -use Leaf\Fetch; |
31 | | - |
32 | | -$res = Fetch::request([ |
33 | | - "url" => 'https://jsonplaceholder.typicode.com/todos/1', |
| 27 | +$res = fetch()->post('https://jsonplaceholder.typicode.com/posts', [ |
| 28 | + 'title' => 'foo', |
| 29 | + 'body' => 'bar', |
| 30 | + 'userId' => 1 |
34 | 31 | ]); |
35 | 32 |
|
36 | | -echo json_encode($res->data); |
37 | | -``` |
38 | | - |
39 | | -Or with Leaf 3's functional mode: |
40 | | - |
41 | | -```php |
42 | | -$res = fetch("https://jsonplaceholder.typicode.com/todos/"); |
| 33 | +fetch()->put(...); |
| 34 | +fetch()->patch(...); |
| 35 | +fetch()->delete(...); |
| 36 | +fetch()->options(...); |
43 | 37 |
|
44 | | -echo json_encode($res->data); |
| 38 | +response()->json($res->data); |
45 | 39 | ``` |
46 | 40 |
|
47 | 41 | ## Installation |
48 | 42 |
|
49 | 43 | You can quickly install leaf fetch with the Leaf CLI |
50 | 44 |
|
51 | 45 | ```sh |
52 | | -# latest stable |
53 | 46 | leaf install fetch |
54 | | - |
55 | | -# dev version |
56 | | -leaf install fetch@dev-main |
57 | 47 | ``` |
58 | 48 |
|
59 | 49 | Or with composer: |
60 | 50 |
|
61 | 51 | ```sh |
62 | | -# latest stable |
63 | 52 | composer require leafs/fetch |
64 | | - |
65 | | -# dev version |
66 | | -composer require leafs/fetch dev-main |
67 | | -``` |
68 | | - |
69 | | -## The `fetch` method |
70 | | - |
71 | | -Leaf fetch provides the fetch method as an easy way to make HTTP requests. This allows you to quickly make requests without bringing up the whole fetch class and without even having to build up your own request array. |
72 | | - |
73 | | -```php |
74 | | -// make a get request |
75 | | -$res = fetch("https://jsonplaceholder.typicode.com/todos/"); |
76 | | - |
77 | | -// make a post request |
78 | | -$res = fetch("https://jsonplaceholder.typicode.com/posts", [ |
79 | | - "title" => "foo", |
80 | | - "body" => "bar", |
81 | | - "userId" => 1, |
82 | | -]); |
83 | | - |
84 | | -// build a custom request array |
85 | | -$res = fetch([ |
86 | | - "method" => "GET", |
87 | | - "url" => 'https://jsonplaceholder.typicode.com/todos/1', |
88 | | - "data" => [ |
89 | | - "firstName" => 'Fred', |
90 | | - "lastName" => 'Flintstone' |
91 | | - ] |
92 | | -]); |
93 | | - |
94 | | -// get response body |
95 | | -echo json_encode($res->data); |
96 | | -``` |
97 | | - |
98 | | -## The `Fetch` class |
99 | | - |
100 | | -The fetch class contains all the options and methods needed to make a network request. |
101 | | - |
102 | | -### baseUrl |
103 | | - |
104 | | -You might have noticed that all the requests above needed us to type a long URL to make the requests, however, we can add a base url so we don't have to type it over and over again. |
105 | | - |
106 | | -```php |
107 | | -Fetch::baseUrl("https://jsonplaceholder.typicode.com"); |
108 | | -``` |
109 | | - |
110 | | -And from there you can make requests like this: |
111 | | - |
112 | | -```php |
113 | | -// make a get request |
114 | | -$res = fetch("/todos"); |
115 | | - |
116 | | -// make a post request |
117 | | -$res = fetch("/posts", [ |
118 | | - "title" => "foo", |
119 | | - "body" => "bar", |
120 | | - "userId" => 1, |
121 | | -]); |
122 | | - |
123 | | -// use the get shortcut method |
124 | | -$res = Fetch::get("/todos/10"); |
125 | | - |
126 | | -// echo response |
127 | | -echo json_encode($res); |
128 | | -``` |
129 | | - |
130 | | -### shortcut methods |
131 | | - |
132 | | -The fetch class comes with shortcut methods named after http methods `get`, `post`, `put`, `patch`, ... |
133 | | - |
134 | | -```php |
135 | | -$res = Fetch::post("/posts", [ |
136 | | - "title" => "foo", |
137 | | - "body" => "bar", |
138 | | - "userId" => 2, |
139 | | -]); |
140 | | - |
141 | | -$res = Fetch::get("/todos/10"); |
142 | | - |
143 | | -Fetch::delete("/todos/10"); |
144 | | - |
145 | | -// ... |
146 | | -``` |
147 | | - |
148 | | -### request |
149 | | - |
150 | | -As you've seen earlier, the fetch class also provides a `request` method which is also used under the hood by the `fetch` function. `request` allows you to manually build up your request object with whatever data you need. |
151 | | - |
152 | | -```php |
153 | | -use Leaf\Fetch; |
154 | | - |
155 | | -$res = Fetch::request([ |
156 | | - "method" => "GET", |
157 | | - "url" => "https://jsonplaceholder.typicode.com/todos", |
158 | | -]); |
159 | | - |
160 | | -echo json_encode($res->data); |
161 | 53 | ``` |
162 | 54 |
|
163 | | -### Request object |
| 55 | +## Options |
164 | 56 |
|
165 | 57 | This is the array which is used to construct the request to be sent. The available fields are: |
166 | 58 |
|
|
0 commit comments