PHP code example of siberfx / laravel-soap

1. Go to this page and download the library: Download siberfx/laravel-soap library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

siberfx / laravel-soap example snippets


use Siberfx\Soap\Facades\Soap;
use Siberfx\Soap\Service;

Soap::add('Currency', fn (Service $service) => $service
    ->wsdl('https://currencyconverter.kowabunga.net/converter.asmx?WSDL')
    ->trace()
);

$amount = Soap::call('Currency.GetConversionAmount', [[
    'CurrencyFrom' => 'USD',
    'CurrencyTo'   => 'EUR',
    'RateDate'     => '2026-09-30',
    'Amount'       => '1000',
]]);

use Siberfx\Soap\Facades\Soap;
use Siberfx\Soap\Service;

public function boot(): void
{
    Soap::add('Weather', function (Service $service) {
        $service
            ->wsdl(config('services.weather.wsdl'))
            ->soapVersion(SOAP_1_2)
            ->basicAuth(config('services.weather.user'), config('services.weather.password'))
            ->connectionTimeout(10)
            ->cache(WSDL_CACHE_BOTH);
    });
}

Soap::add('Weather', [
    'wsdl'  => 'https://example.com/weather?wsdl',
    'trace' => true,
]);

Soap::has('Weather');      // bool
Soap::service('Weather');  // Siberfx\Soap\Service
Soap::services();          // array<string, Service>
Soap::addMany([
    'Weather'  => [...],
    'Currency' => fn (Service $s) => $s->wsdl('...'),
]);

'services' => [
    'currency' => [
        'wsdl'     => env('CURRENCY_WSDL'),
        'trace'    => true,
        'classmap' => [
            'GetConversionAmount' => App\Soap\Types\GetConversionAmount::class,
        ],
        'login'    => env('CURRENCY_USER'),
        'password' => env('CURRENCY_PASSWORD'),
    ],
],

'defaults' => [
    'trace'              => (bool) env('SOAP_TRACE', false),
    'soap_version'       => SOAP_1_1,
    'cache'              => (int) env('SOAP_WSDL_CACHE', WSDL_CACHE_BOTH),
    'connection_timeout' => (int) env('SOAP_CONNECTION_TIMEOUT', 30),
    'verify_ssl'         => (bool) env('SOAP_VERIFY_SSL', true),
],

Soap::add('Legacy', fn (Service $s) => $s
    ->location('https://legacy.example.com/soap')
    ->uri('urn:legacy-service')
);

$result = Soap::call('Service.Method', $arguments, $options, $inputHeaders, $outputHeaders);

// document/literal: one wrapper parameter
Soap::call('Currency.GetConversionAmount', [
    ['CurrencyFrom' => 'USD', 'CurrencyTo' => 'EUR', 'RateDate' => '2026-09-30', 'Amount' => '1000'],
]);

// rpc style / non-WSDL: positional parameters
Soap::call('Calculator.add', [2, 3]);

use Siberfx\Soap\SoapWrapper;

public function transfer(SoapWrapper $soap, array $payload): mixed
{
    $result = $soap->call('Bank.Transfer', [$payload], outputHeaders: $headers);

    $sessionId = $headers['SessionId'] ?? null;

    return $result;
}

namespace App\Soap\Types;

class GetConversionAmount
{
    public function __construct(
        public string $CurrencyFrom,
        public string $CurrencyTo,
        public string $RateDate,
        public string $Amount,
    ) {}
}

class GetConversionAmountResponse
{
    public string $GetConversionAmountResult;
}

Soap::add('Currency', fn (Service $s) => $s
    ->wsdl('https://currencyconverter.kowabunga.net/converter.asmx?WSDL')
    ->classMap([
        'GetConversionAmount'         => GetConversionAmount::class,
        'GetConversionAmountResponse' => GetConversionAmountResponse::class,
    ])
);

/** @var GetConversionAmountResponse $response */
$response = Soap::call('Currency.GetConversionAmount', [
    new GetConversionAmount('USD', 'EUR', '2026-09-30', '1000'),
]);

$response->GetConversionAmountResult;

Soap::add('Secure', fn (Service $s) => $s
    ->wsdl('https://example.com/secure?wsdl')
    ->header('http://example.com/auth', 'AuthHeader', [
        'Username' => config('services.secure.user'),
        'Password' => config('services.secure.password'),
    ], mustUnderstand: true)
);

Soap::call('Secure.GetOrders', [$request], inputHeaders: [
    new SoapHeader('http://example.com/tracing', 'CorrelationId', (string) Str::uuid()),
]);

$client = Soap::client('Currency');

$client->functions();   // operations described by the WSDL
$client->types();       // types described by the WSDL
$client->call('GetCurrencies');
$client->GetCurrencies(); // native magic calls work too

$rates = Soap::client('Currency', fn (SoapClient $client) => $client->call('GetCurrencies'));

Soap::call('Currency.GetCurrencies');

$client = Soap::client('Currency');
$client->lastRequest();
$client->lastRequestHeaders();
$client->lastResponse();
$client->lastResponseHeaders();

use Siberfx\Soap\Exceptions\SoapCallException;

try {
    Soap::call('Bank.Transfer', [$payload]);
} catch (SoapCallException $e) {
    $e->service;        // "Bank"
    $e->method;         // "Transfer"
    $e->faultCode();    // e.g. "soap:Server"
    $e->faultString();  // human readable message from the server
    $e->detail();       // the <detail> element, if any
    $e->lastRequest;    // raw XML (trace enabled)
    $e->lastResponse;   // raw XML (trace enabled)
    $e->fault;          // the original \SoapFault (also getPrevious())
}

use Illuminate\Support\Facades\Event;
use Illuminate\Support\Facades\Log;
use Siberfx\Soap\Events\SoapRequestFailed;
use Siberfx\Soap\Events\SoapResponseReceived;

Event::listen(function (SoapResponseReceived $event) {
    if ($event->duration > 2000) {
        Log::warning("Slow SOAP call {$event->service}.{$event->method}", ['ms' => $event->duration]);
    }
});

Event::listen(function (SoapRequestFailed $event) {
    Log::error($event->exception->getMessage(), [
        'request'  => $event->exception->lastRequest,
        'response' => $event->exception->lastResponse,
    ]);
});

use Siberfx\Soap\Facades\Soap;
use Siberfx\Soap\Testing\RecordedCall;

public function test_it_converts_currency(): void
{
    Soap::fake([
        'Currency.GetConversionAmount' => (object) ['GetConversionAmountResult' => '921.50'],
    ]);

    $this->post('/convert', ['from' => 'USD', 'to' => 'EUR', 'amount' => 1000])
        ->assertOk()
        ->assertSee('921.50');

    Soap::assertCalled('Currency.GetConversionAmount', fn (RecordedCall $call) =>
        $call->argument('0.CurrencyFrom') === 'USD'
    );
}

Soap::fake([
    'Weather.Forecast' => fn (RecordedCall $call) => "Sunny in {$call->argument('0.city')}",
    'Bank.Transfer'    => new SoapFault('Server', 'Insufficient funds'),
    'Currency.*'       => 1.08,
]);

Soap::fake([...])->preventStrayCalls();

Soap::assertCalled('Currency.GetConversionAmount');
Soap::assertCalled('Currency.*', fn (RecordedCall $call) => $call->argument('0.Amount') === '1000');
Soap::assertNotCalled('Bank.Transfer');
Soap::assertCalledTimes('Currency.GetConversionAmount', 2);
Soap::assertNothingCalled();

Soap::recorded();                                         // list<RecordedCall>
Soap::recorded(fn (RecordedCall $call) => $call->service === 'Bank');

use Siberfx\Soap\SoapClient;

class SignedSoapClient extends SoapClient
{
    public function __doRequest(
        string $request,
        string $location,
        string $action,
        int $version,
        bool $oneWay = false,
        ?string $uriParserClass = null, // PHP 8.5+, harmless on 8.4
    ): ?string {
        $signed = app(RequestSigner::class)->sign($request);

        return parent::__doRequest($signed, $location, $action, $version, $oneWay);
    }
}

Soap::add('Government', fn (Service $s) => $s->wsdl('...')->client(SignedSoapClient::class));

$this->app->singleton(\Siberfx\Soap\ClientFactory::class, MyClientFactory::class);
bash
php artisan vendor:publish --tag=soap-config