rymanalu/http

Simple HTTP Client library powered by Guzzle.

v0.2.1 2017-03-30 11:29 UTC

This package is auto-updated.

Last update: 2024-04-09 14:22:26 UTC


README

Build Status

This package provides a new way to use GuzzleHttp package.

Installation

Install this package via the Composer package manager:

composer require rymanalu/http

Usage

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 the Rymanalu\Http\Client instance by giving the BarEndpoint instance:
$endpoint = new BarEndpoint; // or `new BarEndpoint($anArrayOfGuzzleOptions)`

$response = $http->call($endpoint);
  • The $response will be an object of Rymanalu\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