Fetch is a tiny function, wrapped around Guzzle, PHP HTTP client. Inspired by Web API fetch() function.
// 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',
],
]);composer require lemmon/fetchFetch\Response fetch(string $resource, array $init = NULL)$resource- 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 parametermethodto define request method; default method is GET
ok()- (bool) has response been successfulstatus()- (int) status codestatusText()- (string) status textbody()- (string) response bodyjson(bool $assoc = FALSE)- JSON parsed response bodypsr()- (GuzzleHttp\Psr7\Response) Guzzle's PSR-7 response (read more in Guzzle's official documentation)