Guzzle Command
Guzzle Command provides the foundation for building command-based web service
clients on top of Guzzle. A command represents one service operation, and a
result represents the processed response from that operation.
Use this package when you are building an SDK-style client with named operations
such as listUsers() or createOrder(). If you only need to send ordinary HTTP
requests, install
guzzlehttp/guzzle
instead.
For declarative service descriptions that define operations from API metadata,
see
Guzzle Services.
Installation
composer require guzzlehttp/command
Version Guidance
| Version |
Status |
PHP Version |
| 2.0 |
Latest |
>=7.4,<8.7 |
| 1.5 |
Maintenance |
>=7.2.5,<8.7 |
Quick Start
use GuzzleHttp\Client as HttpClient;
use GuzzleHttp\Command\CommandInterface;
use GuzzleHttp\Command\Result;
use GuzzleHttp\Command\ResultInterface;
use GuzzleHttp\Command\ServiceClient;
use GuzzleHttp\Psr7\Request;
use Psr\Http\Message\RequestInterface;
use Psr\Http\Message\ResponseInterface;
$client = new ServiceClient(
new HttpClient(['base_uri' => 'https://api.example.com']),
function (CommandInterface $command): RequestInterface {
return new Request(
'POST',
'/' . rawurlencode($command->getName()),
['Content-Type' => 'application/json'],
\json_encode($command->toArray(), \JSON_THROW_ON_ERROR)
);
},
function (
ResponseInterface $response,
RequestInterface $request,
CommandInterface $command
): ResultInterface {
return new Result(\json_decode((string) $response->getBody(), true, 512, \JSON_THROW_ON_ERROR));
}
);
$result = $client->createUser(['name' => 'Ada']);
The service client can also execute commands asynchronously and run many
commands with a configurable concurrency limit.
Documentation
Security
If you discover a security vulnerability within this package, please send an
email to security@tidelift.com. All security vulnerabilities will be promptly
addressed by the security maintainers listed in the
Security Policy. Please do
not disclose security-related issues publicly until a fix has been announced.
License
Guzzle Command is made available under the MIT License (MIT). Please see
License File for more information.
For Enterprise
Available as part of the Tidelift Subscription
The maintainers of Guzzle and thousands of other packages are working with
Tidelift to deliver commercial support and maintenance for the open source
dependencies you use to build your applications. Save time, reduce risk, and
improve code health, while paying the maintainers of the exact dependencies you
use.
Learn more.