需在 Laravel 项目中使用,依赖框架的 HTTP 客户端与 Artisan;base_uri 需通过 TRANSPORTER_BASE_URI 环境变量或配置文件设置;发送 XML 需在请求类中手动 use SendsXml trait
laravel-transporter
JustSteveKingMIT更新放缓用面向对象方式在 PHP 里发送 API 请求的 Laravel 扩展,把请求封装成类,告别散落各处的 HTTP 调用代码。
Transporter is a futuristic way to send API requests in PHP. This is an OOP approach to handling API requests.
composer require juststeveking/laravel-transporter
php artisan vendor:publish --provider="JustSteveKing\Transporter\TransporterServiceProvider" --tag="transporter-config"README 未说明,这是 PHP/Laravel 包,无特殊硬件要求
把 Laravel 的 HTTP 请求封装成类,用面向对象方式发送 API 请求
用面向对象方式在 PHP 里发送 API 请求的 Laravel 扩展,把请求封装成类,告别散落各处的 HTTP 调用代码。
核心能力
- 请求类继承 Laravel PendingRequest,可直接复用其全部方法
- 用 Artisan 命令 make:api-request 生成请求类
- 通过 Concurrently 门面并发发送多个请求,并支持自定义 key
- 提供 fake 方法伪造单个请求或并发请求的响应
- 可选 SendsXml trait 发送 XML 请求体
容易踩的坑
- 4.0.1最新正式版2026-06-24 · 2026-06-24 08:06 UTC
What's Changed
- Allow nullable Pool parameter in ensureRequest method by @mdpoulter in https://github.com/JustSteveKing/laravel-transporter/pull/54
New Contributors
- @mdpoulter made their first contribution in https://github.com/JustSteveKing/laravel-transporter/pull/54
Full Changelog: https://github.com/JustSteveKing/laravel-transporter/compare/4.0.0...4.0.1
- 4.0.02026-06-24 · 2026-06-24 08:05 UTC
查看更新说明
What's Changed
- Laravel 13.x Compatibility by @laravel-shift in https://github.com/JustSteveKing/laravel-transporter/pull/55
Full Changelog: https://github.com/JustSteveKing/laravel-transporter/compare/3.2.0...4.0.0
- 3.2.02025-05-04 · 2025-05-04 19:27 UTC
查看更新说明
What's Changed
- Laravel 12.x Compatibility by @laravel-shift in https://github.com/JustSteveKing/laravel-transporter/pull/53
Full Changelog: https://github.com/JustSteveKing/laravel-transporter/compare/3.1.0...3.2.0
- 3.1.0Accept Query Parameters for GET requests2024-10-04 · 2024-10-04 07:43 UTC
查看更新说明
What's Changed
- Support query params on GET requests by @edalzell in https://github.com/JustSteveKing/laravel-transporter/pull/52
Full Changelog: https://github.com/JustSteveKing/laravel-transporter/compare/3.0.0...3.1.0
- 3.0.02024-04-22 · 2024-04-22 08:47 UTC
查看更新说明
What's Changed
- Laravel 11.x Compatibility by @laravel-shift in https://github.com/JustSteveKing/laravel-transporter/pull/50
Full Changelog: https://github.com/JustSteveKing/laravel-transporter/compare/2.0.01...3.0.0
用户点评
登录用户的真实使用记录,可按版本和硬件筛选还没有人点评。第一个说出真实体验的人最有价值。
Transporter
Transporter is a futuristic way to send API requests in PHP. This is an OOP approach to handle API requests.

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
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
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
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
$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
$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
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
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
Thanks to a fantastic suggestion by @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:
<?php
declare(strict_types=1);
namespace App\Transporter\Requests;
use JustSteveKing\Transporter\Concerns\SendsXml;
use JustSteveKing\Transporter\Request;
class XmlRequest extends Request
{
use SendsXml;
protected string $method = 'POST';
protected string $path = '/your-endpoint';
}
Then all you need to do is call the methods:
XmlRequest::build()->withXml(
xml: '<todo><name>Send an XML Requets</name><completed>false</completed></todo>'
)->send();
Testing
To run the tests in parallel:
composer run test
To run the tests with a coverage report:
composer run test-coverage
Changelog
Please see CHANGELOG for more information on what has changed recently.
Contributing
Please see CONTRIBUTING for details.
Security Vulnerabilities
Please review our security policy on how to report security vulnerabilities.
Credits
License
The MIT License (MIT). Please see License File for more information.