Skip to content

Commit e4418f7

Browse files
committed
chore: update readme
1 parent 1363e29 commit e4418f7

File tree

1 file changed

+16
-124
lines changed

1 file changed

+16
-124
lines changed

README.md

Lines changed: 16 additions & 124 deletions
Original file line numberDiff line numberDiff line change
@@ -1,166 +1,58 @@
11
<!-- markdownlint-disable no-inline-html -->
22
<p align="center">
33
<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"/>
55
<h1 align="center">Fetch</h1>
66
<br><br>
77
</p>
88

9-
# Leaf Fetchs
10-
119
[![Latest Stable Version](https://poser.pugx.org/leafs/fetch/v/stable)](https://packagist.org/packages/leafs/leaf)
1210
[![Total Downloads](https://poser.pugx.org/leafs/fetch/downloads)](https://packagist.org/packages/leafs/leaf)
1311
[![License](https://poser.pugx.org/leafs/fetch/license)](https://packagist.org/packages/leafs/leaf)
1412

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.
1614

1715
## fetch example
1816

1917
```php
20-
use function Leaf\fetch;
21-
22-
$res = fetch("https://jsonplaceholder.typicode.com/todos/");
18+
$res = fetch('https://jsonplaceholder.typicode.com/todos/');
2319

24-
echo json_encode($res->data);
20+
// data returned is saved in the $data property just like axios
21+
response()->json($res->data);
2522
```
2623

27-
You can also use the fetch class
24+
You can also use the entire fetch object to make requests:
2825

2926
```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
3431
]);
3532

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(...);
4337

44-
echo json_encode($res->data);
38+
response()->json($res->data);
4539
```
4640

4741
## Installation
4842

4943
You can quickly install leaf fetch with the Leaf CLI
5044

5145
```sh
52-
# latest stable
5346
leaf install fetch
54-
55-
# dev version
56-
leaf install fetch@dev-main
5747
```
5848

5949
Or with composer:
6050

6151
```sh
62-
# latest stable
6352
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);
16153
```
16254

163-
### Request object
55+
## Options
16456

16557
This is the array which is used to construct the request to be sent. The available fields are:
16658

0 commit comments

Comments
 (0)