comsave/soap-client
Laravel-friendly SOAP client wrapper that streamlines calling SOAP services in PHP. Provides simple configuration, request/response handling, and integration helpers so you can consume WSDL-based APIs with less boilerplate in your application.
composer require comsave/soap-client
use Comsave\SoapClient\Client;
$client = new Client('http://example.com/soap-service?wsdl');
$response = $client->__soapCall('OperationName', ['param1' => 'value1']);
$client = new Client('https://example.com/wsdl/weather.wsdl');
$weather = $client->__soapCall('GetWeather', ['zipCode' => '12345']);
Client constructor.__soapCall() for direct SOAP operations (mirrors SoapClient).Service Initialization
$client = new Client('https://api.example.com/service?wsdl', [
'trace' => 1, // Enable tracing for debugging
'exceptions' => true, // Throw exceptions on errors
]);
$client = new Client(null, [
'location' => 'https://api.example.com',
'uri' => 'urn:example',
]);
Request/Response Handling
$response = $client->__soapCall('CreateOrder', [
'orderId' => 'ORD123',
'items' => [
['productId' => 'P1', 'quantity' => 2],
['productId' => 'P2', 'quantity' => 1],
],
]);
Error Handling
exceptions in config to catch SOAP faults.
try {
$response = $client->__soapCall('FaultyOperation', []);
} catch (\SoapFault $e) {
// Handle SOAP-specific errors
Log::error($e->getMessage());
}
trace to log raw SOAP requests/responses.
$client->setTrace(true); // Enable globally
$request = $client->__getLastRequest(); // Inspect last request
$response = $client->__getLastResponse(); // Inspect last response
Laravel-Specific Patterns
$this->app->bind('soap.client', function ($app) {
return new Client(config('services.soap.wsdl'), config('services.soap.options'));
});
config/services.php):
'soap' => [
'wsdl' => env('SOAP_WSDL_URL'),
'options' => [
'trace' => env('SOAP_TRACE', false),
'exceptions' => true,
'timeout' => 30,
],
],
// In a facade class:
public static function call($method, $params) {
return app('soap.client')->__soapCall($method, $params);
}
Batch Operations
$client = new Client('https://api.example.com/wsdl');
foreach ($orders as $order) {
$client->__soapCall('ProcessOrder', $order);
}
WSDL Caching
$client = new Client('https://api.example.com/wsdl', [
'cache_wsdl' => WSDL_CACHE_NONE, // Disable caching
]);
Namespace/URI Mismatches
uri (namespace) in options doesn’t match the WSDL.uri in the WSDL and match it in the client config:
$client = new Client(null, [
'location' => 'https://api.example.com',
'uri' => 'urn:example:api', // Must match WSDL
]);
Complex Data Types
$response = $client->__soapCall('GetUser', ['id' => 1]);
$user = (array) $response; // Force array conversion if needed
Timeouts and Performance
timeout and connection_timeout:
$client = new Client('https://api.example.com/wsdl', [
'timeout' => 60, // 60 seconds
'connection_timeout' => 10, // 10 seconds to connect
]);
SOAP Headers
__setSoapHeaders():
$header = new \SoapHeader('urn:auth', 'AuthToken', ['token' => 'abc123']);
$client->__setSoapHeaders([$header]);
Deprecated Methods
SoapClient deprecations in PHP 8+).Enable Tracing
$client->setTrace(true);
$lastRequest = $client->__getLastRequest();
$lastResponse = $client->__getLastResponse();
Validate WSDL
Check PHP SOAP Extensions
soap extension is enabled in php.ini:
extension=soap
Custom Response Handling
class CustomSoapClient extends \Comsave\SoapClient\Client {
public function __soapCall($functionName, $args) {
$response = parent::__soapCall($functionName, $args);
return $this->transformResponse($response);
}
protected function transformResponse($response) {
// Custom logic (e.g., flatten arrays, cast types)
return $response;
}
}
Middleware for Requests
$client->setMiddleware(function ($request) {
$request->headers['X-Custom'] = 'value';
return $request;
});
Laravel Events
$client->beforeCall(function ($method, $params) {
event(new SoapCallStarted($method, $params));
});
How can I help you explore Laravel packages today?