teepluss/consume

This package is not installable via Composer 1.x, please make sure you upgrade to Composer 2+. Read more about our Composer 1.x deprecation policy.

Retrieving data from a REST api without HTTP request.

1.0.0 2016-12-09 04:06 UTC

This package is not auto-updated.

Last update: 2021-07-24 02:08:38 UTC


README

Consume your own API from the same application without request via HTTP protocol

Installation

To get the latest version of Consume simply require it in your composer.json file.

"teepluss/consume": "^1.0.0"

You'll then need to run composer install to download it and have the autoloader updated.

Once Consume is installed you need to register the service provider with the application. Open up config/app.php and find the providers key.

'providers' => [

    Teepluss\Consume\ConsumeServiceProvider::class,

]

Consume also ships with a facade which provides the static syntax for creating collections. You can register the facade in the aliases key of your config/app.php file.

'aliases' => [

    'Consume' => Teepluss\Consume\Facades\Consume::class,

]

Usage

$accessToken = "[YOUR_ACCESS_TOKEN]";

// File uploading.
$userfile = request()->file('userfile');

// POST parameters.
$parameters = [
    'name'     => 'Teepluss',
    'userfile' => $userfile
];

try {
    $request = Consume::asJson()
                      ->withAccessToken($accessToken)
                      ->request('POST', '/api/user', $parameters)
                      ->send();

    $response = $request->getContent();
} catch (\Teepluss\Consume\Exception\ErrorException $e) {
    // This may return laravel validation error.
    $response = $e->getContent();
} catch (\Teepluss\Consume\Exception\NotFoundException $e) {
    $response = 'Not Found Exception';
}

Problem

If you are sending file upload to the REST api you need to get file directly.

// Not work
request()->file('userfile');

// work
request()->files->get('userfile');

Support or Contact

If you have any problems, Contact teepluss@gmail.com

Support via PayPal