besimple/soap-client
Extends PHP’s native SoapClient with support for SOAP with Attachments (SwA), MTOM optimization, WS-Security (incl. UsernameToken), and WS-Addressing. Useful when integrating SOAP services that require attachments and message-level security.
Install via Composer
Add the package to your composer.json:
"require": {
"besimple/soap-client": "^0.2"
}
Run composer install or composer update.
Basic Usage Initialize the client in a Laravel service or controller:
use BeSimple\SoapClient\SoapClient;
$client = new SoapClient(
'https://example.com/soap-service?wsdl',
[
'trace' => 1,
'exceptions' => true,
]
);
First SOAP Call Define a method and call it:
$response = $client->__soapCall('MethodName', [
'param1' => 'value1',
'param2' => 'value2',
]);
Logging (Optional) Enable Laravel's logging to debug SOAP requests/responses:
$client->setLogger(new \Monolog\Logger('soap'));
WS-Security Authentication Secure requests with username/password tokens:
$client = new SoapClient('service.wsdl', [
'wsSecurity' => [
'username' => 'user',
'password' => 'pass',
'passwordType' => 'PasswordText',
],
]);
MTOM for Binary Data Optimize large attachments (e.g., PDFs):
$client = new SoapClient('service.wsdl', [
'mtom' => true,
]);
$response = $client->uploadFile([
'file' => new \SoapParam(file_get_contents('file.pdf'), 'file'),
]);
SwA for Attachments Send files alongside SOAP messages:
$client = new SoapClient('service.wsdl', [
'swA' => true,
]);
$response = $client->sendWithAttachment([
'data' => 'payload',
'attachment' => [
'file.pdf' => file_get_contents('file.pdf'),
],
]);
WS-Addressing Override default endpoints dynamically:
$client = new SoapClient('service.wsdl', [
'wsAddressing' => [
'action' => 'http://example.com/action',
'to' => 'http://example.com/endpoint',
],
]);
Service Providers Bind the client to the container for dependency injection:
$this->app->bind(SoapClient::class, function ($app) {
return new SoapClient(config('soap.wsdl'), config('soap.options'));
});
Config Files
Store WSDL and options in config/soap.php:
return [
'wsdl' => env('SOAP_WSDL', ''),
'options' => [
'trace' => env('SOAP_TRACE', 1),
'wsSecurity' => [
'username' => env('SOAP_USER'),
'password' => env('SOAP_PASS'),
],
],
];
Middleware for Logging Log all SOAP requests/responses:
$client->setLogger(app('log')->channel('soap'));
WS-Security Quirks
passwordType (e.g., PasswordText, PasswordDigest).trace: 1 to verify headers:
$lastRequest = $client->__getLastRequest();
$lastResponse = $client->__getLastResponse();
MTOM/SwA Limitations
memory_limit or soap.wsdl_cache).WS-Addressing Conflicts
wsAddressing headers. Check the WSDL for required actions.Namespace Issues
__setSoapHeaders() for custom headers:
$header = new \SoapHeader('namespace', 'headerName', $data, false, null, null, 'rpc');
$client->__setSoapHeaders($header);
Enable Traces
$client = new SoapClient('service.wsdl', ['trace' => 1]);
var_dump($client->__getLastRequest(), $client->__getLastResponse());
Validate WSDL
Use SoapClient::getFunctions() to list available methods:
print_r($client->getFunctions());
Handle Exceptions Wrap calls in try-catch:
try {
$response = $client->methodName();
} catch (\SoapFault $fault) {
Log::error("SOAP Error: {$fault->faultcode} - {$fault->faultstring}");
}
Custom Headers Extend the client for reusable headers:
class CustomSoapClient extends SoapClient {
public function __construct($wsdl, $options = []) {
$header = new \SoapHeader('ns', 'Auth', ['token' => 'xyz']);
parent::__construct($wsdl, $options + ['exceptions' => true]);
$this->__setSoapHeaders($header);
}
}
Middleware for Pre/Post Processing Intercept requests/responses:
$client->setHandler(function ($request, $response) {
// Modify $request or $response
return [$request, $response];
});
Laravel Facades Create a facade for cleaner syntax:
// app/SoapClient.php
class SoapClient extends \Illuminate\Support\Facades\Facade {
protected static function getFacadeAccessor() {
return 'soap.client';
}
}
How can I help you explore Laravel packages today?