zendframework/zend-soap
Zend Framework’s SOAP component for building SOAP clients and servers in PHP. Includes WSDL generation/consumption, automatic class mapping, and helpers for encoding/decoding and fault handling—useful for integrating legacy SOAP services and APIs.
Installation Add the package via Composer:
composer require zendframework/zend-soap
Register the autoloader in composer.json if not auto-discovered.
First Use Case: Consuming a SOAP Service
Create a simple client in a service class (e.g., app/Services/SoapClientService.php):
use Zend\Soap\Client;
class SoapClientService
{
public function __construct(private string $wsdlUrl)
{
}
public function callService()
{
$client = new Client($this->wsdlUrl);
$result = $client->someMethod(['param1' => 'value']);
return $result;
}
}
Where to Look First
Zend\Soap\Client and Zend\Soap\Server classes in the source.config/app.php for autoloading adjustments if needed.SoapClientService to Laravel’s container in AppServiceProvider:
$this->app->bind(SoapClientService::class, function ($app) {
return new SoapClientService(config('soap.wsdl_url'));
});
config/soap.php:
return [
'wsdl_url' => env('SOAP_WSDL_URL'),
'options' => [
'trace' => true, // Enable for debugging
'exceptions' => true,
],
];
public function fetchData(SoapClientService $soapService)
{
$data = $soapService->callService();
return response()->json($data);
}
use Zend\Soap\Server;
class SoapServerService
{
public function __construct(private string $wsdlPath)
{
}
public function start()
{
$server = new Server(null, [
'uri' => 'http://example.com/soap',
]);
$server->setClass('App\Services\SoapService'); // Class with SOAP methods
$server->handle();
}
}
@soap tags (via PHPDoc or Zend\Soap\AutoDiscover):
/**
* @soap
*/
public function getUserData($userId)
{
return User::find($userId)->toArray();
}
Zend\Soap\AutoDiscover to register complex types:
$autoDiscover = new AutoDiscover();
$autoDiscover->addClassMap('User', User::class);
$server = new Server($autoDiscover);
Zend\Soap\ComplexType for custom structures:
$complexType = new ComplexType('UserArray', ['type' => 'array', 'sequence' => ['User']]);
$autoDiscover->addComplexType($complexType);
try {
$result = $client->someMethod();
} catch (SoapFault $fault) {
Log::error("SOAP Error: {$fault->getMessage()}");
throw new \RuntimeException("SOAP service unavailable");
}
App\Exceptions\Handler: Centralize SOAP-specific exceptions:
public function render($request, Throwable $exception)
{
if ($exception instanceof \SoapFault) {
return response()->json(['error' => $exception->getMessage()], 500);
}
return parent::render($request, $exception);
}
Http facade or Mockery to simulate SOAP calls:
$mockClient = Mockery::mock(Client::class);
$mockClient->shouldReceive('someMethod')->andReturn(['mocked' => 'data']);
$this->app->instance(Client::class, $mockClient);
public function testSoapService()
{
$service = new SoapClientService('http://example.com/wsdl');
$this->assertEquals('expected', $service->callService());
}
$client = new Client($wsdlUrl, [
'cache_wsdl' => WSDL_CACHE_NONE,
]);
$client->setOptions(['trace' => true]);
$request = $client->__getLastRequest();
$response = $client->__getLastResponse();
$client = new Client($wsdlUrl, [
'features' => SOAP_SINGLE_ELEMENT_ARRAYS,
'namespace' => 'urn:example',
]);
soap PHP extension, which may not be enabled by default.php.ini or via Laravel’s .env:
extension=soap
php -m | grep soap.soap.wsdl_cache and soap.wsdl_cache_dir settings, or increase post_max_size and memory_limit in php.ini.$client = new Client($wsdlUrl, [
'connection_timeout' => 30,
'execution_timeout' => 60,
]);
event(new SoapCalled($result));
$header = new SoapHeader('http://example.com/ns', 'Auth', ['token' => 'abc123']);
$client->__setSoapHeaders([$header]);
$autoDiscover = new AutoDiscover();
$autoDiscover->setUri('http://example.com/soap');
$autoDiscover->setClass('App\Services\SoapService');
$server = new Server($autoDiscover->getWsdl());
$client->setOptions(['trace' => true]);
Log::info('SOAP Request:', ['request' => $client->__getLastRequest()]);
Log::info('SOAP Response:', ['response' => $client->__getLastResponse()]);
retry helper or a custom decorator:
$retry = 3;
while ($retry--) {
try {
$result = $client->someMethod();
break;
} catch (SoapFault $fault) {
if ($retry === 0) throw $f
How can I help you explore Laravel packages today?