This package provides a new way to use GuzzleHttp package.
Install this package via the Composer package manager:
composer require rymanalu/http
Case: we want to call http://foo.com/bar
API.
Steps:
- Create a new endpoint by extending
Rymanalu\Http\Endpoint
. For example:
use Rymanalu\Http\Endpoint;
class BarEndpoint extends Endpoint
{
/**
* The endpoint's URI.
*
* @var string
*/
protected $uri = 'bar';
/**
* The endpoint's method.
*
* @var string
*/
protected $method = 'POST';
}
- Create a new
Rymanalu\Http\Client
instance:
$http = new Rymanalu\Http\Client('http://foo.com');
- Call the
call
method in theRymanalu\Http\Client
instance by giving theBarEndpoint
instance:
$endpoint = new BarEndpoint; // or `new BarEndpoint($anArrayOfGuzzleOptions)`
$response = $http->call($endpoint);
- The
$response
will be an object ofRymanalu\Http\Response
:
// Check if the calls is successful by the response code...
$response->isSuccessful(); // `true` or `false`
// Get the response body...
$response->getBody(); // object of `stdClass`
$response->getBody(true); // array