Skip to content
Merged
Show file tree
Hide file tree
Changes from 29 commits
Commits
Show all changes
32 commits
Select commit Hold shift + click to select a range
a4c0e77
Init
sahilsaha7773 Jun 22, 2023
6b3025c
Delete .DS_Store
sahilsaha7773 Jun 22, 2023
f9983aa
Delete .DS_Store
sahilsaha7773 Jun 22, 2023
3a11910
Updated composer.json
sahilsaha7773 Jul 3, 2023
795a6be
Improved code quality
sahilsaha7773 Jul 3, 2023
35813d2
Removed index.php
sahilsaha7773 Jul 10, 2023
f8eff6c
Removed name from authors
sahilsaha7773 Jul 10, 2023
2f42a4e
Added config file for phpstan
sahilsaha7773 Jul 10, 2023
98685a4
Updated the scripts
sahilsaha7773 Jul 10, 2023
272ab12
Removed type from Response
sahilsaha7773 Jul 10, 2023
1e98f86
Removed array() and updated json() and the tests accordingly
sahilsaha7773 Jul 10, 2023
e2eb93e
Updated tests.yml
sahilsaha7773 Jul 10, 2023
b53d232
Added default in match and updated phpstan config
sahilsaha7773 Jul 10, 2023
582e357
Added README.md
sahilsaha7773 Jul 11, 2023
d586ef6
Added more usage examples
sahilsaha7773 Jul 12, 2023
fe26fc6
Added request headers check in tests
sahilsaha7773 Jul 12, 2023
47592c3
Fixed Typo
sahilsaha7773 Jul 30, 2023
20c69a4
Updated tests so that isOk is removed
sahilsaha7773 Aug 1, 2023
de325e9
Updated Client.php
sahilsaha7773 Aug 1, 2023
b27bcd0
Added valid method check and updated default parameters
sahilsaha7773 Aug 1, 2023
2e7ba9b
Fixed code style
sahilsaha7773 Aug 1, 2023
834ee48
Removed url and method from Response
sahilsaha7773 Aug 1, 2023
c36fcc2
Removed default header content-type
sahilsaha7773 Aug 1, 2023
d2509ab
Fixed tests for sending file
sahilsaha7773 Aug 1, 2023
3836ee7
Changed response body to mixed type
sahilsaha7773 Aug 1, 2023
e72b9eb
Added tests for sending more file types and redirects
sahilsaha7773 Aug 1, 2023
35df8d5
Added tests for getting a file as a response
sahilsaha7773 Aug 2, 2023
050edef
rtrim url if it contains ? already before adding query string
sahilsaha7773 Aug 2, 2023
6fa6820
Code quality fixes
sahilsaha7773 Aug 2, 2023
12a7996
Removed unnecessary code and updated variable names
sahilsaha7773 Aug 3, 2023
bcfdc53
Made timeout configurable
sahilsaha7773 Aug 3, 2023
99aeb4f
Updated README.md
sahilsaha7773 Aug 4, 2023
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
21 changes: 21 additions & 0 deletions .github/workflows/codeql-analysis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
name: "CodeQL"
Comment thread
Meldiron marked this conversation as resolved.

on: [ pull_request ]
jobs:
lint:
name: CodeQL
runs-on: ubuntu-latest

steps:
- name: Checkout repository
uses: actions/checkout@v3
with:
fetch-depth: 2

- run: git checkout HEAD^2

- name: Install dependencies
run: composer install --profile --ignore-platform-reqs

- name: Run CodeQL
run: composer check
21 changes: 21 additions & 0 deletions .github/workflows/linter.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
name: "Linter"

on: [ pull_request ]
jobs:
lint:
name: Linter
runs-on: ubuntu-latest

steps:
- name: Checkout repository
uses: actions/checkout@v3
with:
fetch-depth: 2

- run: git checkout HEAD^2

- name: Install dependencies
run: composer install --profile --ignore-platform-reqs

- name: Run Linter
run: composer lint
23 changes: 23 additions & 0 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
name: "Tests"

on: [ pull_request ]
jobs:
lint:
name: Tests
runs-on: ubuntu-latest

steps:
- name: Checkout repository
uses: actions/checkout@v3
with:
fetch-depth: 2

- run: git checkout HEAD^2

- name: Install dependencies
run: composer install --profile --ignore-platform-reqs

- name: Run Tests
run: php -S localhost:8000 tests/router.php &
composer test

3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
vendor
*.cache
composer.lock
112 changes: 112 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
# 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.

The `Client::fetch()` method 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.
- `query` - An **associative array** of query parameters.

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

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

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

$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());
}
```
### POST request
```php
<?php
require_once __DIR__ . '/vendor/autoload.php';
use Utopia\Fetch\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
);

print_r($resp->json());
```
### Send a file
```php
<?php
require_once __DIR__ . '/vendor/autoload.php';
use Utopia\Fetch\Client;

$url = 'http://localhost:8000';
$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
);

print_r($resp->json());
```
26 changes: 26 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
{
"name": "utopia-php/fetch",
"description": "A simple library that provides an interface for making HTTP Requests.",
"type": "library",
"license": "MIT",
"require": {
"php": ">=8.0"
},
"require-dev": {
Comment thread
Meldiron marked this conversation as resolved.
"phpunit/phpunit": "^9.5",
"laravel/pint": "^1.5.0",
"phpstan/phpstan": "^1.10"
},
"scripts": {
"lint": "./vendor/bin/pint --test --config pint.json",
"format": "./vendor/bin/pint --config pint.json",
"check": "./vendor/bin/phpstan analyse -c phpstan.neon",
"test": "./vendor/bin/phpunit --configuration phpunit.xml --debug"
Comment thread
sahilsaha7773 marked this conversation as resolved.
},
"autoload": {
"psr-4": {
"Utopia\\Fetch\\": "src/"
}
},
"authors": []
}
5 changes: 5 additions & 0 deletions phpstan.neon
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
parameters:
level: 8
paths:
- src
- tests
16 changes: 16 additions & 0 deletions phpunit.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<phpunit backupGlobals="false"
backupStaticAttributes="false"
bootstrap="vendor/autoload.php"
colors="true"
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
processIsolation="false"
stopOnFailure="false"
>
<testsuites>
<testsuite name="Application Test Suite">
<directory>./tests/</directory>
</testsuite>
</testsuites>
</phpunit>
3 changes: 3 additions & 0 deletions pint.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"preset": "psr12"
}
Loading