teepluss / laravel-consume by teepluss

Retrieving data from a REST api without HTTP request.
36
4
2
Package Data
Maintainer Username: teepluss
Maintainer Contact: teepluss@gmail.com (Teepluss)
Package Create Date: 2016-12-08
Package Last Update: 2016-12-09
Home Page:
Language: PHP
License: MIT
Last Refreshed: 2024-12-16 15:01:59
Package Statistics
Total Downloads: 36
Monthly Downloads: 0
Daily Downloads: 0
Total Stars: 4
Total Watchers: 2
Total Forks: 0
Total Open Issues: 0

Consume Laravel REST api without HTTP request.

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