econea/nusoap
NuSOAP for Laravel: a maintained PHP SOAP client/server library packaged for modern apps. Call WSDL services, build SOAP requests/responses, and integrate legacy SOAP APIs without the native SOAP extension, with Composer-friendly installation.
## Getting Started
### Minimal Setup
1. **Installation**
```bash
composer require econea/nusoap
First Use Case: SOAP Client
use Econea\NuSoap\Client;
$client = new Client('https://example.com/soap-service?wsdl');
$response = $client->call('MethodName', ['param1' => 'value1']);
print_r($response);
First Use Case: SOAP Server
use Econea\NuSoap\Server;
$server = new Server('https://example.com/soap-service.wsdl');
$server->register('MethodName');
$server->handle();
Where to Look First
src/Econea/NuSoap/ for core classes like Client, Server, and Fault.examples/ directory in the repository (if available) for practical use cases.Instantiation
$client = new Client(
'https://example.com/soap-service?wsdl',
[
'trace' => 1, // Enable request/response logging
'exceptions' => true, // Throw exceptions on errors
'strict_warnings' => true, // Handle undefined output messages (new in v0.9.20)
]
);
Calling Methods
// Simple call
$response = $client->call('GetData', ['id' => 123]);
// Complex call with namespaces
$response = $client->call(
'ComplexMethod',
['param' => ['@attributes' => ['ns' => 'urn:namespace']]],
['namespace' => 'urn:namespace']
);
Handling Responses
if ($client->fault) {
throw new \RuntimeException($client->faultstring);
}
$result = $client->getResponse();
Debugging
// Enable debug output
$client->setDebugLevel(9);
echo '<pre>' . htmlspecialchars($client->getDebug(), ENT_QUOTES) . '</pre>';
Registering Methods
$server = new Server('https://example.com/soap-service.wsdl');
$server->register('GetUserData');
$server->register('UpdateUser', ['in' => 'userId', 'out' => 'userData']);
Handling Requests
$server->setObject(new class {
public function GetUserData($userId) {
return ['id' => $userId, 'name' => 'John Doe'];
}
});
Custom WSDL Handling
$server->wsdl->addComplexType(
'UserData',
'complexType',
'struct',
'sequence',
[
['name' => 'id', 'type' => 'xsd:int'],
['name' => 'name', 'type' => 'xsd:string'],
]
);
Laravel Service Providers Bind the client/server to the container for dependency injection:
$this->app->bind(Client::class, function ($app) {
return new Client(config('services.soap.endpoint'), [
'trace' => $app['config']['services.soap.trace'],
'strict_warnings' => true, // Leverage the new warning handling
]);
});
Middleware for SOAP Requests Use Laravel middleware to validate or transform SOAP requests before they reach the server:
$server->register('SecureMethod', [], ['middleware' => 'validateSoapRequest']);
Caching WSDL Cache the WSDL parsing result to avoid repeated network calls:
$wsdlCache = Cache::remember('soap_wsdl_' . md5($wsdlUrl), 3600, function () use ($wsdlUrl) {
return new Client($wsdlUrl, ['strict_warnings' => true]);
});
PHP Version Compatibility
composer.json constraints or CI checks (e.g., GitHub Actions) to enforce version compatibility.Namespace Conflicts
use Econea\NuSoap\Client as NuSoapClient;
WSDL Generation Issues
Server class to customize WSDL generation:
$server->wsdl->schemaTargetNamespace = 'urn:custom-namespace';
Large Payloads
post_max_size, memory_limit).php.ini or stream large data:
$client->setUseCurl(true);
$client->setCurlOption(CURLOPT_TIMEOUT, 30);
Fault Handling
exceptions is set to false.$client->fault after calls:
if ($client->fault) {
Log::error('SOAP Fault: ' . $client->faultstring);
throw new \RuntimeException($client->faultstring, $client->faultcode);
}
Undefined Output Messages (New in v0.9.20)
strict_warnings in the client configuration to ensure warnings are caught:
$client = new Client($wsdlUrl, ['strict_warnings' => true]);
Enable Full Debugging
$client->setDebugLevel(9); // Max verbosity
echo '<pre>' . htmlspecialchars($client->getDebug(), ENT_QUOTES) . '</pre>';
$client->getLastRequest() and $client->getLastResponse() for raw data.Logging Use Laravel’s logging to persist debug info:
Log::debug('SOAP Request', ['request' => $client->getLastRequest()]);
Common Errors
SOAP-ENV:Server: Server-side error. Check the server’s debug logs.SOAP-ENV:VersionMismatch: WSDL or SOAP version mismatch. Verify the WSDL URL and SOAP version.SOAP-ENV:Client: Client-side error (e.g., invalid parameters). Validate input data.strict_warnings, ensure all output parameters are defined in the WSDL or method signature.Custom Fault Handling
Extend the Fault class to add custom logic:
class CustomFault extends \Econea\NuSoap\Fault {
public function __construct($faultcode, $faultstring, $faultactor = null) {
parent::__construct($faultcode, $faultstring, $faultactor);
// Add custom logic (e.g., logging, retries)
}
}
Middleware for Client Intercept requests/responses:
$client->setMiddleware(function ($request, $response, $client) {
// Pre-process request
$request = str_replace('old', 'new', $request);
return [$request, $response];
});
Custom Transport Layer Replace the default HTTP transport (e.g., for Guzzle or custom HTTP clients):
$client->setUseCurl(false);
$client->setTransport(new CustomTransport());
Testing Use Laravel’s HTTP tests to mock SOAP responses:
$response = Http::fake([
'https://example.com/soap-service' => Http::response($mockXml, 200),
]);
How can I help you explore Laravel packages today?