Installation Add the package via Composer (if available in a repository or forked manually):
composer require guzzle/service
(Note: Since this is a read-only subtree split of Guzzle 3, ensure compatibility with Laravel 4.x or legacy projects.)
Basic Usage The package provides a Service Client abstraction for interacting with RESTful APIs. Start with:
use Guzzle\Service\Client;
use Guzzle\Service\Description\ServiceDescription;
// Load a service description (e.g., from a JSON file or API docs)
$description = ServiceDescription::fromJson(file_get_contents('path/to/service.json'));
// Initialize the client
$client = new Client('https://api.example.com', [
'description' => $description,
'curl' => [CURLOPT_SSL_VERIFYPEER => false] // Disable for testing
]);
First Use Case: Fetching Data Use the client to call an API endpoint:
$response = $client->get('users', ['query' => ['limit' => 10]]);
$users = $response->json();
Service Description Integration
service.json file (e.g., generated from Swagger/OpenAPI).{
"users": {
"get": {
"path": "/users",
"httpMethod": "GET",
"response": {
"200": {
"body": {
"type": "array",
"items": {"type": "object"}
}
}
}
}
}
}
$description = ServiceDescription::fromJson($json);
$client = new Client('https://api.example.com', ['description' => $description]);
Request/Response Handling
$client->get('users/{id}', ['path' => ['id' => 1]]);
$client->getAsync('users')->then(function ($response) {
return $response->json();
});
Middleware and Plugins
$stack = new Guzzle\Service\Middleware();
$stack->push(new Guzzle\Service\Middleware\AuthMiddleware('api_key', 'Bearer'));
$client->setMiddlewareStack($stack);
Laravel Integration
$app->bind('api.client', function () {
$description = ServiceDescription::fromJson(config('services.api.description'));
return new Client(config('services.api.url'), [
'description' => $description,
'headers' => ['Authorization' => 'Bearer ' . $app['auth']->token()]
]);
});
public function index(Client $client) {
$users = $client->get('users')->json();
return view('users.index', compact('users'));
}
Guzzle 3 Compatibility
Service Description Limitations
ServiceDescription class is not auto-generated from modern API specs (e.g., OpenAPI 3). You must manually define the schema or convert from Swagger 1.2.No Built-in Retry Logic
$stack->push(function ($request, $options) {
if ($options['retry'] && $request->getError()) {
return $request->getClient()->send($request, $options);
}
});
SSL Verification
CURLOPT_SSL_VERIFYPEER => false) is unsafe for production. Use proper certificates or a CA bundle:
'curl' => [CURLOPT_CAINFO => __DIR__.'/path/to/cert.pem']
$client->getEmitter()->attach(new Guzzle\Plugin\Log\LogPlugin(null, new \Monolog\Logger('name')));
$response = $client->get('users');
\Log::debug($response->getBody(), ['headers' => $response->getHeaders()]);
Custom Response Parsers Override default JSON/XML parsing:
$client->setResponseParser(function ($response) {
return json_decode($response->getBody(), true);
});
Dynamic Service Descriptions Fetch descriptions at runtime (e.g., from a config file or API):
$description = ServiceDescription::fromJson(file_get_contents(config('api.description_url')));
Legacy Laravel 4 Support If using Laravel 4, bind the client to the IoC container:
App::bind('api', function() {
return new Client('https://api.example.com', ['description' => $this->getDescription()]);
});
How can I help you explore Laravel packages today?