Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: update readme #4

Merged
merged 2 commits into from
Mar 18, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
150 changes: 73 additions & 77 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,113 +1,109 @@
# Utopia Fetch
Lite & fast micro PHP library that provides a convenient and flexible way to perform HTTP requests in PHP applications.

# Usage
The library provides a static method `Client::fetch()` that returns a `Response` object.
## Usage
Create an instance of the `Client` class to perform HTTP requests. The instance methods allow for setting request options like headers, timeout, connection timeout, and more.

The `Client::fetch()` method accepts the following parameters:
The `fetch()` method on a `Client` instance accepts the following parameters:
- `url` - A **String** containing the URL to which the request is sent.
- `method` - A **String** containing the HTTP method for the request. The default method is `GET`.
- `headers` - An **associative array** of HTTP headers to send.
- `body` - An **associative array** of data to send as the body of the request.
- `body` - An **array** of data to send as the body of the request, which can be an associative array for form data or a JSON string.
- `query` - An **associative array** of query parameters.
- `timeout` - An **integer** representing the maximum number of seconds to allow cURL functions to execute, the default is `15`.

The `Response` object has the following methods:
- `isOk()` - Returns **true** if the response status code is in the range 200-299, **false** otherwise.
- `getBody()` - Returns the response body as a **String**.
- `getHeaders()` - Returns an **associative array** of response headers.
- `getStatusCode()` - Returns the response status code as an **integer**.
- `getMethod()` - Returns the request method as a **String**.
- `getUrl()` - Returns the request URL as a **String**.
- `text()` - Returns the response body as a **String**.
- `json()` - Returns the response body as an **associative array**.
- `blob()` - Converts the response body to blob and return it as a **String**.


The `Response` object returned by `fetch()` provides several methods to inspect the response:
- `isOk()` - Checks if the response status code is within the 200-299 range.
- `getBody()` - Retrieves the response body as a string.
- `getHeaders()` - Fetches the response headers as an associative array.
- `getStatusCode()` - Gets the response status code.
- `json()` - Decodes the response body to an associative array.
- `text()` - Alias for `getBody()`, returns the response body as a string.
- `blob()` - Returns the response body as a blob.

## Examples
### GET request

### GET Request
```php
<?php
require_once __DIR__ . '/vendor/autoload.php';
use Utopia\Fetch\Client;

$client = new Client();
$url = 'https://httpbin.org/get';
$method = 'GET';
$query = [
'foo' => 'bar'
];

$resp = Client::fetch(
url: $url,
method: $method,
query: $query
$query = ['foo' => 'bar'];

// Optionally set more configurations
$client
->setUserAgent('CustomUserAgent/1.0')
->setAllowRedirects(true)
->setMaxRedirects(1)
->setConnectionTimeout(10)
->setTimeout(30);

$resp = $client->fetch(
url: $url,
method: $method,
query: $query
);

if($resp->isOk()) {
print("Status Code: " . $resp->getStatusCode() . "\n");
print("Response Headers:\n");
print_r($resp->getHeaders());
print("Response Body:\n");
print($resp->getBody());
}
else {
print("Error: " . $resp->getStatusCode() . "\n");
print("Response Headers:\n");
print_r($resp->getHeaders());
print("Response Body:\n");
print($resp->getBody());
if ($resp->isOk()) {
echo "Status Code: " . $resp->getStatusCode() . "\n";
echo "Response Headers:\n";
print_r($resp->getHeaders());
echo "Response Body:\n";
echo $resp->getBody();
} else {
echo "Error: " . $resp->getStatusCode() . "\n";
}
```
### POST request


### POST Request

```php
<?php
require_once __DIR__ . '/vendor/autoload.php';
use Utopia\Fetch\Client;

$client = new Client();
$url = 'https://httpbin.org/post';
$method = 'POST';
$headers = [
'Content-Type' => 'application/json',
];
$body = [
'name' => 'John Doe',
];
$query = [
'foo' => 'bar'
];

$resp = Client::fetch(
url: $url,
method: $method,
headers: $headers,
body: $body,
query: $query
$headers = ['Content-Type' => 'application/json'];
$body = ['name' => 'John Doe'];
$query = ['foo' => 'bar'];

// Set request headers
$client->addHeader('Content-Type', 'application/json');

$resp = $client->fetch(
url: $url,
method: $method,
body: $body,
query: $query
);

print_r($resp->json());
```
### Send a file

### Sending a file

```php
<?php
require_once __DIR__ . '/vendor/autoload.php';
use Utopia\Fetch\Client;

$url = 'http://localhost:8000';
$client = new Client();
$url = 'http://localhost:8000/upload';
$method = 'POST';
$headers = [
'Content-Type' => 'application/json',
];
$filePath = strval(realpath(__DIR__ . '/tests/resources/logo.png')); // Absolute path to the file

$body = [
'file' => new \CURLFile($filePath, 'image/png', 'logo.png')
];

$resp = Client::fetch(
url: $url,
method: $method,
headers: $headers,
body: $body

// Ensure you set the appropriate Content-Type for file upload
$client->addHeader('Content-Type', 'multipart/form-data');

$filePath = realpath(__DIR__ . '/tests/resources/logo.png'); // Absolute path to the file
$body = ['file' => new \CURLFile($filePath, 'image/png', 'logo.png')];

$resp = $client->fetch(
url: $url,
method: $method,
body: $body
);

print_r($resp->json());
```
```
Loading