Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/lemmon/fetch-php
Tiny function, wrapped around Guzzle, PHP HTTP client. Inspired by Web API fetch() function.
https://github.com/lemmon/fetch-php
curl http psr-7 webservices
Last synced: about 1 month ago
JSON representation
Tiny function, wrapped around Guzzle, PHP HTTP client. Inspired by Web API fetch() function.
- Host: GitHub
- URL: https://github.com/lemmon/fetch-php
- Owner: lemmon
- License: mit
- Created: 2018-08-23T13:11:10.000Z (over 6 years ago)
- Default Branch: master
- Last Pushed: 2019-11-22T16:27:31.000Z (about 5 years ago)
- Last Synced: 2024-04-24T14:29:18.995Z (9 months ago)
- Topics: curl, http, psr-7, webservices
- Language: PHP
- Homepage:
- Size: 4.88 KB
- Stars: 3
- Watchers: 4
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
Fetch, simple PHP HTTP client
=============================Fetch is a tiny function, wrapped around Guzzle, PHP HTTP client. Inspired by Web API fetch() function.
## Examples
```php
// plain GET request
$res = fetch('http://uuid.lemmonjuice.com/');
$body = $res->body(); // returns response body
// get JSON data
$json = fetch('http://uuid.lemmonjuice.com/', [
'headers' => [
'Accept' => 'application/json',
],
])->json();
// POST data
$res = fetch('http://httpbin.org/post', [
'method' => 'POST',
'headers' => [
'Content-Type' => 'application/json',
'Accept' => 'application/json',
],
'json' => [
'hello' => 'world',
],
]);
```## Installing Fetch
```bash
composer require lemmon/fetch
```## API
```php
Fetch\Response fetch(string $resource, array $init = NULL)
```### Parameters
- `$resource` - a resource that you wish to fetch *(e.g. http://httpbin.org/post)*
- `$init` (optional) - options array; see Guzzle's [Request Options](http://docs.guzzlephp.org/en/stable/request-options.html) documentation page for more info about available parameters; note: use additional parameter `method` to define request method; default method is GET### Response
- `ok()` - (bool) has response been successful
- `status()` - (int) status code
- `statusText()` - (string) status text
- `body()` - (string) response body
- `json(bool $assoc = FALSE)` - JSON parsed response body
- `psr()` - (GuzzleHttp\Psr7\Response) Guzzle's PSR-7 response (read more in Guzzle's [official documentation](http://docs.guzzlephp.org/en/stable/psr7.html#responses))## Read more
- [Guzzle][Guzzle] - HTTP client that makes it easy to send HTTP requests and trivial to integrate with web services
- [PSR-7][PSR7] - HTTP message interfaces
- [fetch()][fetch] - Web API fetch()## License
[MIT](https://tldrlegal.com/license/mit-license)
[Guzzle]: https://github.com/guzzle/guzzle
[PSR7]: https://www.php-fig.org/psr/psr-7/
[fetch]: https://developer.mozilla.org/en-US/docs/Web/API/WindowOrWorkerGlobalScope/fetch