PHPackages                             juststeveking/laravel-transporter - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [API Development](/categories/api)
4. /
5. juststeveking/laravel-transporter

ActiveLibrary[API Development](/categories/api)

juststeveking/laravel-transporter
=================================

Transporter is a futuristic way to send API requests in PHP. This is an OOP approach to handle API requests.

3.2.0(1y ago)480195.2k↑18%25[2 PRs](https://github.com/JustSteveKing/laravel-transporter/pulls)2MITPHPPHP ^8.1CI passing

Since May 27Pushed 1y ago5 watchersCompare

[ Source](https://github.com/JustSteveKing/laravel-transporter)[ Packagist](https://packagist.org/packages/juststeveking/laravel-transporter)[ Docs](https://github.com/JustSteveKing/laravel-transporter)[ RSS](/packages/juststeveking-laravel-transporter/feed)WikiDiscussions main Synced 1mo ago

READMEChangelog (10)Dependencies (12)Versions (26)Used By (2)

Transporter
===========

[](#transporter)

[![Latest Version on Packagist](https://camo.githubusercontent.com/924add429c600b4b85fa185c4ff5cb07408bfa3e693f3faffb7ac633133b8f17/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f6a75737473746576656b696e672f6c61726176656c2d7472616e73706f727465722e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/juststeveking/laravel-transporter)[![GitHub Tests Action Status](https://camo.githubusercontent.com/2eb569606bfbdb6d3d70cabaea5ce0af31bb17769b36f71c67f41ca1e09bdfc1/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f616374696f6e732f776f726b666c6f772f7374617475732f4a75737453746576654b696e672f6c61726176656c2d7472616e73706f727465722f72756e2d74657374732e796d6c3f6272616e63683d6d61696e266c6162656c3d5465737473267374796c653d666c61742d737175617265)](https://github.com/JustSteveKing/laravel-transporter/actions?query=workflow%3Arun-tests+branch%3Amain)[![Total Downloads](https://camo.githubusercontent.com/7d09a7d0ccacd10557c92fc9f7d39db0b7b002f8d902b4cc280b3afa2dce2d77/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f6a75737473746576656b696e672f6c61726176656c2d7472616e73706f727465722e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/juststeveking/laravel-transporter)

Transporter is a futuristic way to send API requests in PHP. This is an OOP approach to handle API requests.

[![](banner.png)](banner.png)

Installation
------------

[](#installation)

You can install the package via composer:

```
composer require juststeveking/laravel-transporter
```

You can publish the config file with:

```
php artisan vendor:publish --provider="JustSteveKing\Transporter\TransporterServiceProvider" --tag="transporter-config"
```

The contents of the published config file:

```
return [
    'base_uri' => env('TRANSPORTER_BASE_URI'),
];
```

Generating Request
------------------

[](#generating-request)

To generate an API request to use with Transporter, you can use the Artisan make command:

```
php artisan make:api-request NameOfYourRequest
```

This will by default publish as: `app/Transporter/Requests/NameOfYourRequest.php`

Usage
-----

[](#usage)

Transporter Requests are an extention of Laravels `PendingRequest` so all of the methods available on a Pending Request is available to you on your requests.

Also when you send the request, you will receive a `Illuminate\Http\Client\Response` back, allowing you to do things such as `collect($key)` and `json()` and `failed()` very easily. We are simply just shifting how we send it into a class based approach.

```
TestRequest::build()
    ->withToken('foobar')
    ->withData([
        'title' => 'Build a package'
    ])
    ->send()
    ->json();
```

When building your request to send, you can override the following:

- Request Data using `withData(array $data)`
- Request Query Params using `withQuery(array $query)`
- Request Path using `setPath(string $path)`

### Checking the payload

[](#checking-the-payload)

I had a request in an issue to be able to see the request data for a request, so I have added a helper method called `payload` which will return whatever has been stored in the request `data` property.

```
$request = TestRequest::build()
    ->withToken('foobar')
    ->withData([
        'title' => 'Build a package'
    ]);

$data = $request->payload(); // ['title' => 'Build a package']
```

### Concurrent Requests

[](#concurrent-requests)

```
$responses = \JustSteveKing\Transporter\Facades\Concurrently::build()->setRequests([
    TestRequest::build()
        ->withToken('foobar')
        ->withData([
        'title' => 'Build a package'
    ]),
    TestRequest::build()
        ->withToken('foobar')
        ->withData([
        'title' => 'Build a package'
    ]),
    TestRequest::build()
        ->withToken('foobar')
        ->withData([
        'title' => 'Build a package'
    ]),
]);

$responses[0]->json();
$responses[1]->json();
$responses[2]->json();
```

### Concurrency with a Custom key

[](#concurrency-with-a-custom-key)

```
$responses = \JustSteveKing\Transporter\Facades\Concurrently::build()->setRequests([
    TestRequest::build()
        ->as(
            key: 'first'
        )
        ->withToken('foobar')
        ->withData([
        'title' => 'Build a package'
    ]),
    TestRequest::build()
        ->as(
            key: 'second'
        )
        ->withToken('foobar')
        ->withData([
        'title' => 'Build a package'
    ]),
    TestRequest::build()
        ->as(
            key: 'third'
        )
        ->withToken('foobar')
        ->withData([
        'title' => 'Build a package'
    ]),
]);

$responses['first']->json();
$responses['second']->json();
$responses['third']->json();
```

### Optional Alias

[](#optional-alias)

Instead of the standard `send()` method, it is also possible to use the fun alias `energize()`. *Please note, no sound effects are included.*

```
TestRequest::build()
    ->withToken('foobar')
    ->withData([
        'title' => 'Build a package'
    ])
    ->energize()
    ->json();
```

### Faking a Request or Concurrent

[](#faking-a-request-or-concurrent)

To fake a request, all you need to do is replace the build method with the fake method, which takes an optional `status` parameter, to set the status code being returned with the response:

```
TestRequest::fake(
    status: 200,
)->withToken('foobar')
->withData([
    'title' => 'Build a package'
])->withFakeData([
    'data' => 'faked'
])->send();
```

```
$responses = Concurrently::fake()->setRequests([
    TestRequest::fake()->setPath(
        path: '/todos/1',
    )->as(
        key: 'first'
    ),
    TestRequest::fake()->setPath(
        path: '/todos/2',
    )->as(
        key: 'second'
    ),
    TestRequest::fake()->setPath(
        path: '/todos/3',
    )->as(
        key: 'thirds'
    ),
])->run();
```

Which will return a response with the data you pass through to `withFakeData`, which internally will merge what is on the class with what you pass it. So you can build up an initial state of faked data per class.

### Sending XML

[](#sending-xml)

Thanks to a fantastic suggestion by [@jessarcher](https://github.com/jessarcher) we can use a `Trait` to allow for easy use of XML in your requests. Using this as a trait makes a lot of sense as most APIs these days use JSON, so it is purely opt in. To use this, simply use the trait on your request:

```
