amf/webservices-client-bundle
Installation:
composer require amf/webservices-client-bundle
Enable the bundle in config/bundles.php:
return [
// ...
Amf\WebservicesClientBundle\AmfWebservicesClientBundle::class => ['all' => true],
];
Configuration:
Define services in config/packages/amf_webservices_client.yaml:
amf_webservices_client:
services:
my_soap_service:
type: soap
wsdl: 'http://example.com/soap?wsdl'
options:
trace: true
my_rest_service:
type: rest
base_uri: 'http://example.com/api'
options:
headers:
Authorization: 'Bearer token123'
First Use Case: Inject the client in a service/controller and call a SOAP method:
use Amf\WebservicesClientBundle\Service\WebservicesClient;
class MyService
{
public function __construct(private WebservicesClient $client) {}
public function fetchData()
{
$result = $this->client->call('my_soap_service', 'GetData', ['param1' => 'value1']);
return $result;
}
}
Service Definition:
Define SOAP services in YAML with WSDL and optional options (e.g., trace, exceptions).
my_soap_service:
type: soap
wsdl: 'https://api.example.com/wsdl'
options:
trace: true
exceptions: true
Method Invocation: Call SOAP methods dynamically:
$response = $this->client->call('my_soap_service', 'MethodName', [
'param1' => 'value1',
'param2' => 123
]);
Response Handling:
Process responses (XML/JSON) using Symfony’s Serializer:
$data = $this->client->getSerializer()->decode($response, 'xml');
Endpoint Configuration: Define REST endpoints with base URIs and headers:
my_rest_service:
type: rest
base_uri: 'https://api.example.com/v1'
options:
headers:
Accept: 'application/json'
Authorization: 'Bearer token123'
HTTP Method Calls: Use named methods for clarity:
// GET
$response = $this->client->get('my_rest_service', '/users');
// POST
$response = $this->client->post('my_rest_service', '/users', [
'name' => 'John Doe',
'email' => 'john@example.com'
]);
Dynamic Paths/Queries: Use placeholders for dynamic routes:
$response = $this->client->get('my_rest_service', '/users/{id}', [
'id' => 42
]);
Dependency Injection:
Bind the WebservicesClient to a custom interface for better testability:
interface WebservicesClientInterface {
public function call(string $serviceName, string $method, array $params);
}
Error Handling: Centralize error handling via middleware or a decorator:
$this->client->call('my_soap_service', 'FaultyMethod', [])
->then(fn($result) => $result)
->otherwise(fn($error) => $this->handleError($error));
Logging:
Enable trace in SOAP options and log responses for debugging:
options:
trace: true
SOAP WSDL Caching:
php bin/console cache:clear
REST Authentication:
ParameterBag or environment variables:
options:
headers:
Authorization: '%env(API_TOKEN)%'
XML/JSON Parsing:
Serializer to decode:
$data = $this->client->getSerializer()->decode($response, 'xml');
Timeouts:
options:
timeout: 30
SOAP Traces:
Enable trace in options to log raw SOAP requests/responses:
options:
trace: true
Check logs in var/log/dev.log.
REST HTTP Status Codes: Inspect raw responses for HTTP errors:
$response = $this->client->get('my_rest_service', '/users');
if ($response->getStatusCode() !== 200) {
throw new \RuntimeException('API Error');
}
Validation: Validate YAML configuration for typos in service names or WSDL paths.
Custom Serializers:
Override the default Serializer by binding a custom service:
services:
my_serializer:
class: MyApp\Serializer\CustomSerializer
tags: ['serializer.encoder.xml']
Middleware:
Add HTTP middleware (e.g., retry logic) by extending WebservicesClient:
class CustomWebservicesClient extends WebservicesClient
{
protected function createClient(string $serviceName): ClientInterface
{
$client = parent::createClient($serviceName);
$client->setHandlerStack(HandlerStack::create([
new RetryMiddleware(),
]));
return $client;
}
}
Dynamic Service Loading:
Load services dynamically at runtime (e.g., from a database) by extending ServiceLoader.
How can I help you explore Laravel packages today?