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

Soap Client Laravel Package

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.

View on GitHub
Deep Wiki
Context7

Getting Started

Minimal Setup in Laravel

  1. Install via Composer Add the package to your composer.json:

    "require": {
        "besimple/soap-client": "^0.2"
    }
    

    Run composer install or composer update.

  2. 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,
        ]
    );
    
  3. First SOAP Call Define a method and call it:

    $response = $client->__soapCall('MethodName', [
        'param1' => 'value1',
        'param2' => 'value2',
    ]);
    
  4. Logging (Optional) Enable Laravel's logging to debug SOAP requests/responses:

    $client->setLogger(new \Monolog\Logger('soap'));
    

Implementation Patterns

Common Workflows

  1. WS-Security Authentication Secure requests with username/password tokens:

    $client = new SoapClient('service.wsdl', [
        'wsSecurity' => [
            'username' => 'user',
            'password' => 'pass',
            'passwordType' => 'PasswordText',
        ],
    ]);
    
  2. 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'),
    ]);
    
  3. 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'),
        ],
    ]);
    
  4. WS-Addressing Override default endpoints dynamically:

    $client = new SoapClient('service.wsdl', [
        'wsAddressing' => [
            'action' => 'http://example.com/action',
            'to' => 'http://example.com/endpoint',
        ],
    ]);
    

Integration with Laravel

  • 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'));
    

Gotchas and Tips

Pitfalls

  1. WS-Security Quirks

    • Some services require passwordType (e.g., PasswordText, PasswordDigest).
    • Test with trace: 1 to verify headers:
      $lastRequest = $client->__getLastRequest();
      $lastResponse = $client->__getLastResponse();
      
  2. MTOM/SwA Limitations

    • Not all SOAP servers support MTOM/SwA. Fall back to base64 encoding if needed.
    • Large attachments may hit PHP memory limits (memory_limit or soap.wsdl_cache).
  3. WS-Addressing Conflicts

    • Some services ignore custom wsAddressing headers. Check the WSDL for required actions.
  4. Namespace Issues

    • SOAP namespaces in requests must match the WSDL. Use __setSoapHeaders() for custom headers:
      $header = new \SoapHeader('namespace', 'headerName', $data, false, null, null, 'rpc');
      $client->__setSoapHeaders($header);
      

Debugging Tips

  • 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}");
    }
    

Extension Points

  1. 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);
        }
    }
    
  2. Middleware for Pre/Post Processing Intercept requests/responses:

    $client->setHandler(function ($request, $response) {
        // Modify $request or $response
        return [$request, $response];
    });
    
  3. 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';
        }
    }
    
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