Http
Laraquick\Helpers\Http
This is a wrapper around the Guzzle library to simplify usage.
Making a request
Requests follow the same pattern as Guzzle - http://docs.guzzlephp.org/en/stable/quickstart.html#making-a-request except that the methods are called statically.
<?php
use Laraquick\Helpers\Http;
Http::GET('http://example.com');
Http::POST('http://example.com', [
'json' => [
'name' => 'John Doe'
]
]);
Http::request('GET', 'http://example.com');
Responses
This is where this class diverges from Guzzle. While Guzzle returns a whole bunch of verbose information, Http request methods return the body of response sent by the server.
JSON responses are parsed while every other server responses are passed out raw.
Errors and Status codes
Since request methods only return the server responses, Http provides method hasErrors()
to get the check if the response is an error (with response code that's equal or greater than 400) and method getStatusCode()
to fetch the status code of the last request.
<?php
use Laraquick\Helpers\Http;
$resp = Http::GET('http://api.example.com');
if (Http::hasErrors()) {
// Do something
}
// get status code of response
Http::getStatusCode();
The response of the request can be accessed again via method response()
.
Guzzle Client Object
The client object is available through method client()
.
Guzzle's Response Object
The verbose response from Guzzle is provided via method rawResponse()
.
Method response()
is available and returns the server response formatted response
Updated over 6 years ago