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.
composer require guzzlehttp/command| Version | Status | PHP Version |
|---|---|---|
| 2.0 | Latest | >=7.4,<8.6 |
| 1.5 | Maintenance | >=7.2.5,<8.6 |
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.
- Service Clients
- Executing Commands
- Async and Concurrency
- Middleware: Extending the Client
- Upgrade Guide
- Changelog
If you discover a security vulnerability within this package, please send an email to security@tidelift.com. All security vulnerabilities will be promptly addressed. Please do not disclose security-related issues publicly until a fix has been announced. Please see Security Policy for more information.
Guzzle Command is made available under the MIT License (MIT). Please see License File for more information.
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.