Weave Code
Code Weaver
Helps Laravel developers discover, compare, and choose open-source packages. See popularity, security, maintainers, and scores at a glance to make better decisions.
Feedback
Share your thoughts, report bugs, or suggest improvements.
Subject
Message

Zend Soap Laravel Package

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.

View on GitHub
Deep Wiki
Context7

Getting Started

Minimal Setup in Laravel

  1. Installation Add the package via Composer:

    composer require zendframework/zend-soap
    

    Register the autoloader in composer.json if not auto-discovered.

  2. 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;
        }
    }
    
  3. Where to Look First

    • Zend SOAP Documentation (archived but still relevant).
    • Zend\Soap\Client and Zend\Soap\Server classes in the source.
    • Laravel’s config/app.php for autoloading adjustments if needed.

Implementation Patterns

Common Workflows

1. SOAP Client Integration

  • Dependency Injection: Bind the SoapClientService to Laravel’s container in AppServiceProvider:
    $this->app->bind(SoapClientService::class, function ($app) {
        return new SoapClientService(config('soap.wsdl_url'));
    });
    
  • Configuration: Store WSDL URLs and endpoints in config/soap.php:
    return [
        'wsdl_url' => env('SOAP_WSDL_URL'),
        'options' => [
            'trace' => true, // Enable for debugging
            'exceptions' => true,
        ],
    ];
    
  • Usage in Controllers:
    public function fetchData(SoapClientService $soapService)
    {
        $data = $soapService->callService();
        return response()->json($data);
    }
    

2. SOAP Server Integration

  • Define a SOAP Server Class:
    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();
        }
    }
    
  • Expose SOAP Methods: Annotate methods in your service class with @soap tags (via PHPDoc or Zend\Soap\AutoDiscover):
    /**
     * @soap
     */
    public function getUserData($userId)
    {
        return User::find($userId)->toArray();
    }
    

3. Handling Complex Types

  • Custom SOAP Types: Extend Zend\Soap\AutoDiscover to register complex types:
    $autoDiscover = new AutoDiscover();
    $autoDiscover->addClassMap('User', User::class);
    $server = new Server($autoDiscover);
    
  • Arrays and Objects: Use Zend\Soap\ComplexType for custom structures:
    $complexType = new ComplexType('UserArray', ['type' => 'array', 'sequence' => ['User']]);
    $autoDiscover->addComplexType($complexType);
    

4. Error Handling

  • Global Exception Handling: Wrap SOAP calls in a try-catch block:
    try {
        $result = $client->someMethod();
    } catch (SoapFault $fault) {
        Log::error("SOAP Error: {$fault->getMessage()}");
        throw new \RuntimeException("SOAP service unavailable");
    }
    
  • Laravel’s 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);
    }
    

5. Testing SOAP Services

  • Mock SOAP Responses: Use Laravel’s 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);
    
  • Unit Tests: Test SOAP service classes in isolation:
    public function testSoapService()
    {
        $service = new SoapClientService('http://example.com/wsdl');
        $this->assertEquals('expected', $service->callService());
    }
    

Gotchas and Tips

Pitfalls and Debugging

1. WSDL Caching Issues

  • Problem: Zend SOAP caches WSDL definitions aggressively, leading to stale data.
  • Fix: Disable caching or set a short TTL:
    $client = new Client($wsdlUrl, [
        'cache_wsdl' => WSDL_CACHE_NONE,
    ]);
    
  • Debugging: Enable trace logging to inspect raw SOAP requests/responses:
    $client->setOptions(['trace' => true]);
    $request = $client->__getLastRequest();
    $response = $client->__getLastResponse();
    

2. Namespace Conflicts

  • Problem: SOAP services often require specific XML namespaces, which Zend SOAP may mangle.
  • Fix: Explicitly set namespaces in the client options:
    $client = new Client($wsdlUrl, [
        'features' => SOAP_SINGLE_ELEMENT_ARRAYS,
        'namespace' => 'urn:example',
    ]);
    

3. PHP Extensions

  • Problem: Zend SOAP requires the soap PHP extension, which may not be enabled by default.
  • Fix: Enable the extension in php.ini or via Laravel’s .env:
    extension=soap
    
  • Check: Verify with php -m | grep soap.

4. Large Payloads

  • Problem: SOAP services may reject large requests due to server-side limits.
  • Fix: Adjust PHP’s soap.wsdl_cache and soap.wsdl_cache_dir settings, or increase post_max_size and memory_limit in php.ini.

5. Timeouts and Performance

  • Problem: SOAP calls may hang or timeout, especially with slow services.
  • Fix: Configure timeouts in the client options:
    $client = new Client($wsdlUrl, [
        'connection_timeout' => 30,
        'execution_timeout' => 60,
    ]);
    

6. Laravel Service Provider Quirks

  • Problem: Zend SOAP classes are not Laravel-aware (e.g., no event dispatching).
  • Fix: Wrap SOAP logic in Laravel’s context (e.g., dispatch events after calls):
    event(new SoapCalled($result));
    

Extension Points

1. Custom SOAP Headers

  • Use Case: Add authentication or metadata to SOAP requests.
  • Implementation:
    $header = new SoapHeader('http://example.com/ns', 'Auth', ['token' => 'abc123']);
    $client->__setSoapHeaders([$header]);
    

2. Dynamic WSDL Generation

  • Use Case: Generate WSDL on-the-fly for your SOAP server.
  • Implementation:
    $autoDiscover = new AutoDiscover();
    $autoDiscover->setUri('http://example.com/soap');
    $autoDiscover->setClass('App\Services\SoapService');
    $server = new Server($autoDiscover->getWsdl());
    

3. Logging SOAP Traffic

  • Use Case: Log all SOAP requests/responses for auditing.
  • Implementation: Use Laravel’s logging facade:
    $client->setOptions(['trace' => true]);
    Log::info('SOAP Request:', ['request' => $client->__getLastRequest()]);
    Log::info('SOAP Response:', ['response' => $client->__getLastResponse()]);
    

4. Retry Logic

  • Use Case: Implement retries for transient SOAP failures.
  • Implementation: Use Laravel’s retry helper or a custom decorator:
    $retry = 3;
    while ($retry--) {
        try {
            $result = $client->someMethod();
            break;
        } catch (SoapFault $fault) {
            if ($retry === 0) throw $f
    
Weaver

How can I help you explore Laravel packages today?

Conversation history is not saved when not logged in.
Prompt
Add packages to context
No packages found.
jayeshmepani/jpl-moshier-ephemeris-php
elnasnato/laraliveui
labrodev/rest-sdk
sampaui/sampaui
babelqueue/php-sdk
facebook/capi-param-builder-php
babelqueue/symfony
hamzi/corewatch
minionfactory/raw-hydrator
hexters/coinpayment
rjcodes/rjcms
act-training/laravel-permissions-manager
alimarchal/laravel-chart-of-accounts
babenkoivan/elastic-scout-driver
mkwebdesign/filament-watchdog-v5
renatomarinho/laravel-page-speed
zedmagdy/filament-business-hours
renatovdemoura/blade-elements-ui
devgeek/beacon-admin
benjamin-rqt/data-watcher-bundle