microsoft/kiota-http-guzzle
Guzzle-based HTTP library for Kiota-generated PHP SDKs. Provides the runtime HTTP adapter used by Kiota clients to send requests to API endpoints. Install via Composer: microsoft/kiota-http-guzzle.
Install the package in your Laravel project:
composer require microsoft/kiota-http-guzzle
Ensure your composer.json requires Guzzle HTTP (^7.4.5 or higher).
Generate a Kiota client (if not already done): Use the Kiota CLI or manually scaffold a client from OpenAPI/Swagger specs.
Configure the HTTP adapter:
use Microsoft\Kiota\Abstractions\Http\IHttpRequestAdapter;
use Microsoft\Kiota\Http\Guzzle\GuzzleHttpRequestAdapter;
$adapter = new GuzzleHttpRequestAdapter();
$client = new YourGeneratedClient($adapter);
First API call:
$response = $client->getGroups()->get();
$groups = $response->getValue();
GuzzleHttpRequestAdapter: Core class for HTTP requests.ApiException for HTTP errors.Leverage Kiota’s middleware stack to customize requests/responses. Example: Add an auth middleware:
use Microsoft\Kiota\Abstractions\Http\IHttpPipelinePolicy;
use Microsoft\Kiota\Http\Guzzle\GuzzleHttpRequestAdapter;
use GuzzleHttp\Psr7\Request;
class BearerTokenPolicy implements IHttpPipelinePolicy
{
public function __construct(private string $token) {}
public function beforeSendRequest(Request $request): Request
{
return $request->withHeader('Authorization', 'Bearer ' . $this->token);
}
}
// Usage:
$adapter = new GuzzleHttpRequestAdapter();
$adapter->addPolicy(new BearerTokenPolicy('your-token-here'));
Pass a custom Guzzle client to the adapter:
use GuzzleHttp\Client;
$guzzleClient = new Client([
'base_uri' => 'https://api.example.com',
'timeout' => 10.0,
]);
$adapter = new GuzzleHttpRequestAdapter($guzzleClient);
Use sendAsync() for non-blocking calls (requires PHP 8.1+):
$promise = $client->getGroups()->getAsync();
$response = $promise->wait();
Catch ApiException for HTTP errors:
try {
$response = $client->getGroups()->get();
} catch (ApiException $e) {
// Access raw response headers/body via $e->getResponse()
logger()->error('API Error:', ['status' => $e->getStatusCode()]);
}
Enable debug headers (requires HeadersInspectionHandler):
$adapter->addPolicy(new HeadersInspectionHandler());
PHP Version Requirement:
Guzzle Version Lock:
^7.4.5. Mismatches may cause runtime errors.composer.json:
"guzzlehttp/guzzle": "^7.4.5"
Query Parameter Decoding:
parameterNamesDecodingHandler only decodes parameter names, not values.Empty 2xx Responses:
DELETE requests). Kiota handles this, but Laravel’s HTTP clients may not.sendAsync() to inspect raw responses if needed.Middleware Order Matters:
$guzzleClient = new Client(['debug' => fopen('guzzle.log', 'w+')]);
HeadersInspectionHandler to log raw headers:
$adapter->addPolicy(new HeadersInspectionHandler());
$response = $client->getGroups()->getAsync()->wait();
$body = $response->getBody(); // Raw response body
Custom Error Mapping:
Override ApiException behavior by extending ErrorMappingHandler:
use Microsoft\Kiota\Http\Guzzle\Handlers\ErrorMappingHandler;
class CustomErrorHandler extends ErrorMappingHandler
{
protected function getErrorMapping(): array
{
return [
'401' => new CustomUnauthorizedException(),
// ...
];
}
}
Tracing Support: Integrate with OpenTelemetry:
use Microsoft\Kiota\Http\Guzzle\Handlers\TracingHandler;
$adapter->addPolicy(new TracingHandler());
Retry Logic: Use Guzzle’s retry middleware:
$guzzleClient = new Client([
'handler' => HandlerStack::create([
new RetryMiddleware([
'max_retries' => 3,
'delay' => 100,
]),
]),
]);
AppServiceProvider:
$this->app->bind(IHttpRequestAdapter::class, function ($app) {
return new GuzzleHttpRequestAdapter(
new Client(['base_uri' => config('services.api.base_uri')])
);
});
$guzzleClient = new Client([
'on_stats' => function (TransferStats $stats) {
Cache::put("api_{$stats->getHandler()->getUri()}", $stats->getBody(), now()->addMinutes(5));
},
]);
How can I help you explore Laravel packages today?