Skip to content
Tiny function, wrapped around Guzzle, PHP HTTP client. Inspired by Web API fetch() function.
Branch: master
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
src
.gitignore
LICENSE
README.md
composer.json

README.md

Fetch, simple PHP HTTP client

Fetch is a tiny function, wrapped around Guzzle, PHP HTTP client. Inspired by Web API fetch() function.

Examples

// 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

composer require lemmon/fetch

API

Fetch\Response fetch(string $input, array $init = NULL)

Parameters

  • $input - a resource that you wish to fetch (e.g. http://httpbin.org/post)
  • $init (optional) - options array; see Guzzle's Request Options 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)

Read more

  • Guzzle - HTTP client that makes it easy to send HTTP requests and trivial to integrate with web services
  • PSR-7 - HTTP message interfaces
  • fetch() - Web API fetch()

License

MIT

You can’t perform that action at this time.
You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session.